-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
179 lines (135 loc) · 5.09 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <filesystem>
#include <fstream>
#include <string_view>
#include <xargs.hpp>
#include <string_utils.h>
#include <yaml-cpp/yaml.h>
#include "commands.h"
#include "common.h"
#include "context.h"
namespace app {
inline auto apply_general_options( const std::vector<std::string> &options ) {
for ( const auto &opt : options ) {
if ( opt == OPT_VERBOSE ) {
log_level = LOG_LEVEL_DEBUG;
}
}
}
static auto dispatch_command( bs::context &ctx, std::string_view command, std::string_view arguments,
const std::vector<std::string> &options ) {
apply_general_options( options );
if ( command == CMD_INIT ) {
if ( commands::default_init( ctx, arguments ) )
return EXIT_SUCCESS;
commands::failed_command_message( command, "Couldn't init" );
}
if ( command == CMD_BUILD ) {
if ( commands::build_all( ctx, arguments ) )
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
if ( command == CMD_NEW ) {
commands::create_target( arguments );
return EXIT_SUCCESS;
}
if ( command == CMD_ADD ) {
return EXIT_SUCCESS;
}
if ( command == CMD_SHOW ) {
return EXIT_SUCCESS;
}
if ( command == CMD_INSTALL ) {
return EXIT_SUCCESS;
}
if ( command == CMD_HELP ) {
if ( commands::help( arguments ) )
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
if ( command == CMD_SEARCH ) {
commands::search_package( arguments );
return EXIT_SUCCESS;
}
if ( command == CMD_CLEAN ) {
if ( commands::clean( ctx, arguments ) )
return EXIT_SUCCESS;
commands::failed_command_message( command, "Couldn't clean" );
}
if ( command == CMD_CLEAN_ALL ) {
if ( commands::clean_all( ctx ) )
return EXIT_SUCCESS;
commands::failed_command_message( command, "Couldn't clean all" );
}
if ( command == CMD_RUN_TARGET ) {
if ( commands::run_target( ctx, arguments ) )
return EXIT_SUCCESS;
commands::failed_command_message( command, "Couldn't run target" );
}
commands::invalid_command_message( command );
return EXIT_FAILURE;
}
static auto init( bs::context &ctx ) {
namespace fs = std::filesystem;
const auto default_build_path = fs::current_path( ) / common::DEFAULT_BUILD_DIR;
if ( ctx.build_path.empty( ) ) {
ctx.build_path = default_build_path;
}
}
} // namespace app
volatile int log_level = LOG_LEVEL_INFO;
extern int main( int argc, char *argv[] ) {
using namespace std;
string command;
string arguments;
vector<string> options;
app::bs::context context;
xargs::args args;
args.add_arg( "<command>", "Command",
[&]( const auto &v ) {
const auto res = app::commands::is_command( v );
if ( res ) {
command = v;
}
return res;
} )
.add_arg( "<args>", "Arguments",
[&]( const auto &v ) {
arguments = v;
return true;
} )
.add_option( OPT_HELP, OPT_HELP_DESCRIPTION,
[&]( ) {
puts( args.usage( argv[0] ).c_str( ) );
exit( EXIT_SUCCESS );
} )
.add_option( OPT_VERSION, OPT_VERSION_DESCRIPTION,
[&]( ) {
fprintf( stdout, "%s %s\n", APP_NAME, APP_VERSION );
exit( EXIT_SUCCESS );
} )
.add_option( OPT_BINARY, OPT_BINARY_DESCRIPTION, [&]( ) { options.push_back( OPT_BINARY ); } )
.add_option( "--build", "",
[&]( const std::string &val ) {
if ( !val.empty( ) ) {
context.profile_name = string_utils::to_upper_copy( val );
}
return true;
} )
.add_option( OPT_VERBOSE, OPT_VERBOSE_DESCRIPTION, [&]( ) { options.push_back( OPT_VERBOSE ); } )
.add_option( OPT_JOBS, OPT_JOBS_DESCRIPTION, [&]( const std::string &val ) {
if ( !val.empty( ) ) {
const auto res = string_utils::to_int( val );
if ( res )
context.jobs = static_cast<size_t>( res.value( ) );
}
return true;
} );
args.dispath( argc, argv );
if ( static_cast<size_t>( argc ) < 2 ) {
puts( args.usage( argv[0] ).c_str( ) );
return EXIT_SUCCESS;
}
LOG_DEBUG( APP_TAG, "Running command: %1 %2 %3", command, arguments, options );
app::init( context );
return app::dispatch_command( context, command, arguments, options );
}