|
1 | 1 | #include "cppship/cmd/install.h" |
2 | 2 |
|
3 | 3 | #include <cstdlib> |
| 4 | +#include <functional> |
4 | 5 |
|
5 | 6 | #include <gsl/narrow> |
| 7 | +#include <range/v3/range/conversion.hpp> |
| 8 | +#include <range/v3/view/transform.hpp> |
6 | 9 | #include <spdlog/spdlog.h> |
7 | 10 |
|
8 | 11 | #include "cppship/cmd/build.h" |
| 12 | +#include "cppship/core/layout.h" |
9 | 13 | #include "cppship/core/manifest.h" |
| 14 | +#include "cppship/exception.h" |
10 | 15 | #include "cppship/util/fs.h" |
11 | 16 | #include "cppship/util/log.h" |
12 | 17 |
|
13 | 18 | using namespace cppship; |
14 | 19 |
|
| 20 | +namespace { |
| 21 | + |
| 22 | +int do_install(const cmd::BuildContext& ctx, std::span<std::string> binaries, std::string_view prefix) |
| 23 | +{ |
| 24 | + for (const auto& bin : binaries) { |
| 25 | + const auto bin_file = ctx.profile_dir / bin; |
| 26 | + if (!fs::exists(bin_file)) { |
| 27 | + warn("binary {} not found", bin_file.string()); |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + const auto dst = fmt::format("{}/bin/{}", prefix, bin); |
| 32 | + status("install", "{} to {}", bin_file.string(), dst); |
| 33 | + fs::copy_file(bin_file, dst, fs::copy_options::overwrite_existing); |
| 34 | + } |
| 35 | + |
| 36 | + return EXIT_SUCCESS; |
| 37 | +} |
| 38 | + |
| 39 | +} |
| 40 | + |
15 | 41 | int cmd::run_install([[maybe_unused]] const InstallOptions& options) |
16 | 42 | { |
17 | 43 | // TODO(someone): fix me |
18 | 44 | #ifdef _WINNDOWS |
19 | 45 | throw Error { "install is not supported in Windows" }; |
20 | 46 | #else |
21 | | - const int result = run_build({ .profile = options.profile }); |
22 | | - if (result != 0) { |
23 | | - return EXIT_FAILURE; |
| 47 | + BuildContext ctx(options.profile); |
| 48 | + if (ctx.manifest.is_workspace()) { |
| 49 | + throw Error { "install for workspace is not supported" }; |
24 | 50 | } |
25 | 51 |
|
26 | | - BuildContext ctx(options.profile); |
27 | | - Manifest manifest(ctx.metafile); |
28 | | - if (manifest.is_workspace()) { |
29 | | - throw Error { "install for workspace is not supported now" }; |
| 52 | + const auto& layout = ctx.workspace.as_package(); |
| 53 | + if (options.binary && !layout.binary(*options.binary).has_value()) { |
| 54 | + throw InvalidCmdOption { "--bin", fmt::format("specified binary {} is not found", *options.binary) }; |
30 | 55 | } |
31 | 56 |
|
32 | | - const auto bin_file = ctx.profile_dir / manifest.get_if_package()->name(); |
33 | | - if (!fs::exists(bin_file)) { |
34 | | - warn("no binary to install"); |
35 | | - return EXIT_SUCCESS; |
| 57 | + const int result = run_build({ .profile = options.profile, .cmake_target = options.binary }); |
| 58 | + if (result != 0) { |
| 59 | + return EXIT_FAILURE; |
36 | 60 | } |
37 | 61 |
|
38 | | - const auto dst = fmt::format("/usr/local/bin/{}", manifest.get_if_package()->name()); |
39 | | - status("install", "{} to {}", bin_file.string(), dst); |
40 | | - fs::copy_file(bin_file, dst, fs::copy_options::overwrite_existing); |
41 | | - return EXIT_SUCCESS; |
| 62 | + std::vector<std::string> binaries = std::invoke([&] { |
| 63 | + if (options.binary) { |
| 64 | + return std::vector<std::string> { *options.binary }; |
| 65 | + } |
| 66 | + |
| 67 | + const auto tmp = layout.binaries(); |
| 68 | + return tmp | ranges::views::transform(&Target::name) | ranges::to<std::vector>(); |
| 69 | + }); |
| 70 | + |
| 71 | + return do_install(ctx, binaries, "/usr/local"); |
42 | 72 | #endif |
43 | 73 | } |
0 commit comments