1- use anyhow:: Result ;
1+ use crate :: cargo_make:: CargoMake ;
2+ use crate :: docker:: DockerContainer ;
3+ use crate :: project;
4+ use crate :: tools:: { install_tools, tools_tempdir} ;
5+ use anyhow:: { Context , Result } ;
26use clap:: Parser ;
3- use std:: path:: PathBuf ;
7+ use log:: debug;
8+ use std:: fs;
9+ use std:: path:: { Path , PathBuf } ;
10+ use tempfile:: TempDir ;
11+ use tokio:: fs:: { remove_dir_all, remove_file} ;
412
513#[ derive( Debug , Parser ) ]
614pub ( crate ) enum BuildCommand {
@@ -25,10 +33,95 @@ pub(crate) struct BuildVariant {
2533 /// The architecture to build for.
2634 #[ clap( long = "arch" , default_value = "x86_64" ) ]
2735 arch : String ,
36+
37+ /// The variant to build.
38+ variant : String ,
2839}
2940
3041impl BuildVariant {
3142 pub ( super ) async fn run ( & self ) -> Result < ( ) > {
32- Ok ( ( ) )
43+ let project = project:: load_or_find_project ( self . project_path . clone ( ) ) . await ?;
44+ let token = project. token ( ) ;
45+ let tempdir = tools_tempdir ( ) ?;
46+ install_tools ( & tempdir) . await ?;
47+ let makefile_path = tempdir. path ( ) . join ( "Makefile.toml" ) ;
48+ // A temporary directory in the `build` directory
49+ let build_temp_dir = TempDir :: new_in ( project. project_dir ( ) )
50+ . context ( "Unable to create a tempdir for Twoliter's build" ) ?;
51+ let packages_dir = build_temp_dir. path ( ) . join ( "sdk_rpms" ) ;
52+ fs:: create_dir_all ( & packages_dir) ?;
53+
54+ let sdk_container = DockerContainer :: new (
55+ format ! ( "sdk-{}" , token) ,
56+ project
57+ . sdk ( & self . arch )
58+ . context ( format ! (
59+ "No SDK defined in {} for {}" ,
60+ project. filepath( ) . display( ) ,
61+ & self . arch
62+ ) ) ?
63+ . uri ( ) ,
64+ )
65+ . await ?;
66+ sdk_container
67+ . cp_out ( Path :: new ( "twoliter/alpha/build/rpms" ) , & packages_dir)
68+ . await ?;
69+
70+ let rpms_dir = project. project_dir ( ) . join ( "build" ) . join ( "rpms" ) ;
71+ fs:: create_dir_all ( & rpms_dir) ?;
72+ debug ! ( "Moving rpms to build dir" ) ;
73+ for maybe_file in fs:: read_dir ( packages_dir. join ( "rpms" ) ) ? {
74+ let file = maybe_file?;
75+ debug ! ( "Moving '{}'" , file. path( ) . display( ) ) ;
76+ fs:: rename ( file. path ( ) , rpms_dir. join ( file. file_name ( ) ) ) ?;
77+ }
78+
79+ let mut created_files = Vec :: new ( ) ;
80+
81+ let sbkeys_dir = project. project_dir ( ) . join ( "sbkeys" ) ;
82+ if !sbkeys_dir. is_dir ( ) {
83+ // Create a sbkeys directory in the main project
84+ debug ! ( "sbkeys dir not found. Creating a temporary directory" ) ;
85+ fs:: create_dir_all ( & sbkeys_dir) ?;
86+ sdk_container
87+ . cp_out (
88+ Path :: new ( "twoliter/alpha/sbkeys/generate-local-sbkeys" ) ,
89+ & sbkeys_dir,
90+ )
91+ . await ?;
92+ } ;
93+
94+ // TODO: Remove once models is no longer conditionally compiled.
95+ // Create the models directory for the sdk to mount
96+ let models_dir = project. project_dir ( ) . join ( "sources/models" ) ;
97+ if !models_dir. is_dir ( ) {
98+ debug ! ( "models source dir not found. Creating a temporary directory" ) ;
99+ fs:: create_dir_all ( & models_dir. join ( "src/variant" ) )
100+ . context ( "Unable to create models source directory" ) ?;
101+ created_files. push ( models_dir)
102+ }
103+
104+ // Hold the result of the cargo make call so we can clean up the project directory first.
105+ let res = CargoMake :: new ( & project, & self . arch ) ?
106+ . env ( "TWOLITER_TOOLS_DIR" , tempdir. path ( ) . display ( ) . to_string ( ) )
107+ . env ( "BUILDSYS_ARCH" , & self . arch )
108+ . env ( "BUILDSYS_VARIANT" , & self . variant )
109+ . env ( "BUILDSYS_SBKEYS_DIR" , sbkeys_dir. display ( ) . to_string ( ) )
110+ . makefile ( makefile_path)
111+ . project_dir ( project. project_dir ( ) )
112+ . exec ( "build" )
113+ . await ;
114+
115+ // Clean up all of the files we created
116+ for file_name in created_files {
117+ let added = Path :: new ( & file_name) ;
118+ if added. is_file ( ) {
119+ remove_file ( added) . await ?;
120+ } else if added. is_dir ( ) {
121+ remove_dir_all ( added) . await ?;
122+ }
123+ }
124+
125+ res
33126 }
34127}
0 commit comments