Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/cppship/cmd/install.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once

#include "cppship/core/profile.h"
#include <optional>

namespace cppship::cmd {

struct InstallOptions {
Profile profile = Profile::debug;
std::string root;
};

int run_install(const InstallOptions& options);

}
}
12 changes: 4 additions & 8 deletions include/cppship/util/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ class ScopedCurrentDir {
fs::path mPrevCwd;
};

inline void create_if_not_exist(const fs::path& path)
{
if (fs::exists(path)) {
return;
}
void create_if_not_exist(const fs::path& path);

fs::create_directory(path);
}
inline constexpr std::string_view kCppShipDirName = ".cppship";
fs::path get_cppship_dir();

}
}
9 changes: 6 additions & 3 deletions lib/cmd/install.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cstdlib>
#include <filesystem>
#include <thread>

#include <gsl/narrow>
Expand Down Expand Up @@ -32,9 +33,11 @@ int cmd::run_install([[maybe_unused]] const InstallOptions& options)
return EXIT_SUCCESS;
}

const auto dst = fmt::format("/usr/local/bin/{}", manifest.name());
status("install", "{} to {}", bin_file.string(), dst);
const auto root = fs::path(options.root);
const auto dst = root / "bin" / manifest.name();
cppship::create_if_not_exist(dst.parent_path());
status("install", "{} to {}", bin_file.string(), dst.string());
fs::copy_file(bin_file, dst, fs::copy_options::overwrite_existing);
return EXIT_SUCCESS;
#endif
}
}
17 changes: 17 additions & 0 deletions lib/util/fs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "cppship/util/fs.h"
#include <cstdlib>

void cppship::create_if_not_exist(const fs::path& path)
{
if (fs::exists(path)) {
return;
}

if (path.has_parent_path()) {
create_if_not_exist(path.parent_path());
}

fs::create_directory(path);
}

fs::path cppship::get_cppship_dir() { return fs::path(std::getenv("HOME")) / kCppShipDirName; }
11 changes: 7 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "cppship/cppship.h"
#include "cppship/exception.h"
#include "cppship/util/fs.h"
#include "cppship/util/log.h"

#ifndef CPPSHIP_VERSION
Expand Down Expand Up @@ -159,16 +160,18 @@ std::list<SubCommand> build_commands(const ArgumentParser& common)

// install
auto& install = commands.emplace_back("install", common, [](const ArgumentParser& cmd) {
return cmd::run_install({
.profile = parse_profile(cmd.get("--profile")),
});
return cmd::run_install({ .profile = parse_profile(cmd.get("--profile")), .root = cmd.get("--root") });
});

install.parser.add_description("install binary if exists");
install.parser.add_argument("--profile")
.help("build with specific profile")
.metavar("profile")
.default_value(std::string { kProfileRelease });
install.parser.add_argument("--root")
.help("specify the installation root")
.metavar("root")
.default_value(get_cppship_dir().string());

// run
auto& run = commands.emplace_back("run", common, [](const ArgumentParser& cmd) {
Expand Down Expand Up @@ -322,4 +325,4 @@ try {
} catch (const std::exception& e) {
error("unknown error {}", e.what());
return EXIT_FAILURE;
}
}
39 changes: 38 additions & 1 deletion tests/util/fs.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <filesystem>
#include <gtest/gtest.h>
#include <iostream>

#include "cppship/util/fs.h"

Expand All @@ -16,4 +18,39 @@ TEST(fs, ScopedCurrentDir)
}

EXPECT_EQ(fs::current_path(), cwd);
}
}

TEST(fs, CreateIfNotExist)
{
const auto tmpdir = fs::temp_directory_path();
const auto testdir = fs::path("cppship-unitests");

ScopedCurrentDir guard(tmpdir);

if (fs::exists(testdir)) {
fs::remove_all(testdir);
}
fs::create_directory(testdir);

{
ScopedCurrentDir guard2(testdir);
const auto newdir = fs::path("subdir1");
EXPECT_FALSE(fs::exists(newdir));
cppship::create_if_not_exist(newdir);
EXPECT_TRUE(fs::exists(newdir));
}
{
ScopedCurrentDir guard2(testdir);
const auto newdir = fs::path("./subdir2/subsubdir1");
EXPECT_FALSE(fs::exists(newdir));
cppship::create_if_not_exist(newdir);
EXPECT_TRUE(fs::exists(newdir));
}
{
ScopedCurrentDir guard2(testdir);
const auto newdir = fs::path("subdir3/subsubdir1/subsubsubdir1");
EXPECT_FALSE(fs::exists(newdir));
cppship::create_if_not_exist(newdir);
EXPECT_TRUE(fs::exists(newdir));
}
}