5
5
6
6
"github.com/bazelbuild/buildtools/build"
7
7
"github.com/rmohr/bazeldnf/cmd/template"
8
+ "github.com/rmohr/bazeldnf/pkg/api"
9
+ "github.com/rmohr/bazeldnf/pkg/api/bazeldnf"
8
10
"github.com/rmohr/bazeldnf/pkg/bazel"
9
11
"github.com/rmohr/bazeldnf/pkg/reducer"
10
12
"github.com/rmohr/bazeldnf/pkg/repo"
@@ -21,22 +23,117 @@ type rpmtreeOpts struct {
21
23
workspace string
22
24
toMacro string
23
25
buildfile string
26
+ configname string
27
+ lockfile string
24
28
name string
25
29
public bool
26
30
forceIgnoreRegex []string
27
31
}
28
32
29
33
var rpmtreeopts = rpmtreeOpts {}
30
34
35
+ type Handler interface {
36
+ Process (pkgs []* api.Package , arch string , buildfile * build.File ) error
37
+ Write () error
38
+ }
39
+
40
+ type MacroHandler struct {
41
+ bzl , defName string
42
+ bzlfile * build.File
43
+ }
44
+
45
+ func NewMacroHandler (toMacro string ) (Handler , error ) {
46
+ bzl , defName , err := bazel .ParseMacro (rpmtreeopts .toMacro )
47
+
48
+ if err != nil {
49
+ return nil , err
50
+ }
51
+
52
+ bzlfile , err := bazel .LoadBzl (bzl )
53
+ if err != nil {
54
+ return nil , err
55
+ }
56
+
57
+ return & MacroHandler {
58
+ bzl : bzl ,
59
+ bzlfile : bzlfile ,
60
+ defName : defName ,
61
+ }, nil
62
+ }
63
+
64
+ func (h * MacroHandler ) Process (pkgs []* api.Package , arch string , buildfile * build.File ) error {
65
+ if err := bazel .AddBzlfileRPMs (h .bzlfile , h .defName , pkgs , arch ); err != nil {
66
+ return err
67
+ }
68
+
69
+ bazel .PruneBzlfileRPMs (buildfile , h .bzlfile , h .defName )
70
+ return nil
71
+ }
72
+
73
+ func (h * MacroHandler ) Write () error {
74
+ return bazel .WriteBzl (false , h .bzlfile , h .bzl )
75
+ }
76
+
77
+ type WorkspaceHandler struct {
78
+ workspace string
79
+ workspacefile * build.File
80
+ }
81
+
82
+ func NewWorkspaceHandler (workspace string ) (Handler , error ) {
83
+ workspacefile , err := bazel .LoadWorkspace (workspace )
84
+ if err != nil {
85
+ return nil , err
86
+ }
87
+
88
+ return & WorkspaceHandler {
89
+ workspace : workspace ,
90
+ workspacefile : workspacefile ,
91
+ }, nil
92
+ }
93
+
94
+ func (h * WorkspaceHandler ) Process (pkgs []* api.Package , arch string , buildfile * build.File ) error {
95
+ if err := bazel .AddWorkspaceRPMs (h .workspacefile , pkgs , arch ); err != nil {
96
+ return err
97
+ }
98
+
99
+ bazel .PruneWorkspaceRPMs (buildfile , h .workspacefile )
100
+ return nil
101
+ }
102
+
103
+ func (h * WorkspaceHandler ) Write () error {
104
+ return bazel .WriteWorkspace (false , h .workspacefile , h .workspace )
105
+ }
106
+
107
+ type LockFileHandler struct {
108
+ filename string
109
+ config * bazeldnf.Config
110
+ }
111
+
112
+ func NewLockFileHandler (configname , filename string ) (Handler , error ) {
113
+ return & LockFileHandler {
114
+ filename : filename ,
115
+ config : & bazeldnf.Config {
116
+ Name : configname ,
117
+ RPMs : []bazeldnf.RPM {},
118
+ },
119
+ }, nil
120
+ }
121
+
122
+ func (h * LockFileHandler ) Process (pkgs []* api.Package , arch string , buildfile * build.File ) error {
123
+ return bazel .AddConfigRPMs (h .config , pkgs , arch )
124
+ }
125
+
126
+ func (h * LockFileHandler ) Write () error {
127
+ return bazel .WriteLockFile (h .config , h .filename )
128
+ }
129
+
31
130
func NewRpmTreeCmd () * cobra.Command {
32
131
33
132
rpmtreeCmd := & cobra.Command {
34
133
Use : "rpmtree" ,
35
134
Short : "Writes a rpmtree rule and its rpmdependencies to bazel files" ,
36
135
Args : cobra .MinimumNArgs (1 ),
37
136
RunE : func (cmd * cobra.Command , required []string ) error {
38
- writeToMacro := rpmtreeopts .toMacro != ""
39
-
40
137
repos , err := repo .LoadRepoFiles (rpmtreeopts .repofiles )
41
138
if err != nil {
42
139
return err
@@ -68,54 +165,42 @@ func NewRpmTreeCmd() *cobra.Command {
68
165
if err != nil {
69
166
return err
70
167
}
71
- workspace , err := bazel .LoadWorkspace (rpmtreeopts .workspace )
168
+
169
+ var handler Handler
170
+ var configname string
171
+
172
+ if rpmtreeopts .toMacro != "" {
173
+ handler , err = NewMacroHandler (rpmtreeopts .toMacro )
174
+ } else if rpmtreeopts .lockfile != "" {
175
+ configname = rpmtreeopts .configname
176
+ handler , err = NewLockFileHandler (
177
+ rpmtreeopts .configname ,
178
+ rpmtreeopts .lockfile ,
179
+ )
180
+ } else {
181
+ handler , err = NewWorkspaceHandler (rpmtreeopts .workspace )
182
+ }
183
+
72
184
if err != nil {
73
185
return err
74
186
}
75
- var bzlfile * build.File
76
- var bzl , defName string
77
- if writeToMacro {
78
- bzl , defName , err = bazel .ParseMacro (rpmtreeopts .toMacro )
79
- if err != nil {
80
- return err
81
- }
82
- bzlfile , err = bazel .LoadBzl (bzl )
83
- if err != nil {
84
- return err
85
- }
86
- }
187
+
87
188
build , err := bazel .LoadBuild (rpmtreeopts .buildfile )
88
189
if err != nil {
89
190
return err
90
191
}
91
- if writeToMacro {
92
- err = bazel .AddBzlfileRPMs (bzlfile , defName , install , rpmtreeopts .arch )
93
- if err != nil {
94
- return err
95
- }
96
- } else {
97
- err = bazel .AddWorkspaceRPMs (workspace , install , rpmtreeopts .arch )
98
- if err != nil {
99
- return err
100
- }
101
- }
102
- bazel .AddTree (rpmtreeopts .name , build , install , rpmtreeopts .arch , rpmtreeopts .public )
103
- if writeToMacro {
104
- bazel .PruneBzlfileRPMs (build , bzlfile , defName )
105
- } else {
106
- bazel .PruneWorkspaceRPMs (build , workspace )
192
+ bazel .AddTree (rpmtreeopts .name , configname , build , install , rpmtreeopts .arch , rpmtreeopts .public )
193
+
194
+ if err := handler .Process (install , rpmtreeopts .arch , build ); err != nil {
195
+ return err
107
196
}
197
+
108
198
logrus .Info ("Writing bazel files." )
109
- err = bazel . WriteWorkspace ( false , workspace , rpmtreeopts . workspace )
199
+ err = handler . Write ( )
110
200
if err != nil {
111
201
return err
112
202
}
113
- if writeToMacro {
114
- err = bazel .WriteBzl (false , bzlfile , bzl )
115
- if err != nil {
116
- return err
117
- }
118
- }
203
+
119
204
err = bazel .WriteBuild (false , build , rpmtreeopts .buildfile )
120
205
if err != nil {
121
206
return err
@@ -136,6 +221,8 @@ func NewRpmTreeCmd() *cobra.Command {
136
221
rpmtreeCmd .Flags ().StringVarP (& rpmtreeopts .workspace , "workspace" , "w" , "WORKSPACE" , "Bazel workspace file" )
137
222
rpmtreeCmd .Flags ().StringVarP (& rpmtreeopts .toMacro , "to-macro" , "" , "" , "Tells bazeldnf to write the RPMs to a macro in the given bzl file instead of the WORKSPACE file. The expected format is: macroFile%defName" )
138
223
rpmtreeCmd .Flags ().StringVarP (& rpmtreeopts .buildfile , "buildfile" , "b" , "rpm/BUILD.bazel" , "Build file for RPMs" )
224
+ rpmtreeCmd .Flags ().StringVar (& rpmtreeopts .configname , "configname" , "rpms" , "config name to use in lockfile" )
225
+ rpmtreeCmd .Flags ().StringVar (& rpmtreeopts .lockfile , "lockfile" , "" , "lockfile for RPMs" )
139
226
rpmtreeCmd .Flags ().StringVar (& rpmtreeopts .name , "name" , "" , "rpmtree rule name" )
140
227
rpmtreeCmd .Flags ().StringArrayVar (& rpmtreeopts .forceIgnoreRegex , "force-ignore-with-dependencies" , []string {}, "Packages matching these regex patterns will not be installed. Allows force-removing unwanted dependencies. Be careful, this can lead to hidden missing dependencies." )
141
228
rpmtreeCmd .MarkFlagRequired ("name" )
0 commit comments