55
66 "github.com/bazelbuild/buildtools/build"
77 "github.com/rmohr/bazeldnf/cmd/template"
8+ "github.com/rmohr/bazeldnf/pkg/api"
9+ "github.com/rmohr/bazeldnf/pkg/api/bazeldnf"
810 "github.com/rmohr/bazeldnf/pkg/bazel"
911 "github.com/rmohr/bazeldnf/pkg/reducer"
1012 "github.com/rmohr/bazeldnf/pkg/repo"
@@ -21,22 +23,117 @@ type rpmtreeOpts struct {
2123 workspace string
2224 toMacro string
2325 buildfile string
26+ configname string
27+ lockfile string
2428 name string
2529 public bool
2630 forceIgnoreRegex []string
2731}
2832
2933var rpmtreeopts = rpmtreeOpts {}
3034
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+
31130func NewRpmTreeCmd () * cobra.Command {
32131
33132 rpmtreeCmd := & cobra.Command {
34133 Use : "rpmtree" ,
35134 Short : "Writes a rpmtree rule and its rpmdependencies to bazel files" ,
36135 Args : cobra .MinimumNArgs (1 ),
37136 RunE : func (cmd * cobra.Command , required []string ) error {
38- writeToMacro := rpmtreeopts .toMacro != ""
39-
40137 repos , err := repo .LoadRepoFiles (rpmtreeopts .repofiles )
41138 if err != nil {
42139 return err
@@ -68,54 +165,42 @@ func NewRpmTreeCmd() *cobra.Command {
68165 if err != nil {
69166 return err
70167 }
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+
72184 if err != nil {
73185 return err
74186 }
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+
87188 build , err := bazel .LoadBuild (rpmtreeopts .buildfile )
88189 if err != nil {
89190 return err
90191 }
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
107196 }
197+
108198 logrus .Info ("Writing bazel files." )
109- err = bazel . WriteWorkspace ( false , workspace , rpmtreeopts . workspace )
199+ err = handler . Write ( )
110200 if err != nil {
111201 return err
112202 }
113- if writeToMacro {
114- err = bazel .WriteBzl (false , bzlfile , bzl )
115- if err != nil {
116- return err
117- }
118- }
203+
119204 err = bazel .WriteBuild (false , build , rpmtreeopts .buildfile )
120205 if err != nil {
121206 return err
@@ -136,6 +221,8 @@ func NewRpmTreeCmd() *cobra.Command {
136221 rpmtreeCmd .Flags ().StringVarP (& rpmtreeopts .workspace , "workspace" , "w" , "WORKSPACE" , "Bazel workspace file" )
137222 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" )
138223 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" )
139226 rpmtreeCmd .Flags ().StringVar (& rpmtreeopts .name , "name" , "" , "rpmtree rule name" )
140227 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." )
141228 rpmtreeCmd .MarkFlagRequired ("name" )
0 commit comments