Skip to content

Commit

Permalink
feat: add example and init subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Aug 11, 2024
1 parent e8cbfa0 commit 37c593b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 6 deletions.
32 changes: 29 additions & 3 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ env:
# libcxx version to match whats already installed
# https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2404-Readme.md
LLVM_VERSION: '18.1.3'
CPP2B_LIBCXX_BUILD_ROOT: '/tmp/llvm-project/build'

jobs:
typos-check:
Expand Down Expand Up @@ -61,7 +62,7 @@ jobs:
CC=clang-18 CXX=clang++-18 cmake -G Ninja -S runtimes -B build -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind"
ninja -C build
- uses: actions/checkout@v4
- run: CPP2B_LIBCXX_BUILD_ROOT=/tmp/llvm-project/build CC=clang-18 ./build.sh
- run: CC=clang-18 ./build.sh
- uses: actions/upload-artifact@v4
with:
name: cpp2b
Expand All @@ -83,5 +84,30 @@ jobs:
with:
name: cpp2b
- run: chmod +x cpp2b
- run: CPP2B_LIBCXX_BUILD_ROOT=/tmp/llvm-project/build ./cpp2b build
- run: CPP2B_LIBCXX_BUILD_ROOT=/tmp/llvm-project/build ./.cache/cpp2/bin/cpp2b build
- run: ./cpp2b build
- run: ./.cache/cpp2/bin/cpp2b build

build-examples:
name: build examples
needs: [build-script-windows, build-script-linux]
runs-on: ${{ matrix.os }}
strategy:
matrix:
example:
- init
os:
- windows-latest
- ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
id: cpp2b-binary
with:
name: 'cpp2b-${{ matrix.os }}'
- uses: actions/cache/restore@v4
if: "'${{ matrix.os }}' == 'ubuntu-24.04'"
with:
path: /tmp/llvm-project/build
key: 'libcxx-${{ env.LLVM_VERSION }}'
- run: '${{ steps.cpp2b-binary.outputs.download-path }} build'
working-directory: "examples/${{ matrix.example }}"
2 changes: 2 additions & 0 deletions examples/init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# cpp2b cache directory
.cache/cpp2
5 changes: 5 additions & 0 deletions examples/init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This project is the result of running

```bash
cpp2b init
```
6 changes: 6 additions & 0 deletions examples/init/build.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cpp2b.build;

build: (inout b: cpp2b::build) -> void = {
// rename binary so instead of it being 'src/main' it will be 'init'
b.binary_name("src/main", "init");
}
1 change: 1 addition & 0 deletions examples/init/src/main.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main: () = std::println("Hello, init!");
65 changes: 62 additions & 3 deletions src/main.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ log_info: <Args...: type>(fmt: std::format_string<Args...>, forward args...: Arg
std::println("\033[0;36mINFO\033[0m: {}", std::format(fmt, args...));
}

log_success: <Args...: type>(fmt: std::format_string<Args...>, forward args...: Args) = {
std::println("\033[0;32mSUCCESS\033[0m: {}", std::format(fmt, args...));
}

ensure_dir: (dir: fs::path) = {
status := fs::status(dir);

Expand Down Expand Up @@ -358,6 +362,7 @@ main: (args) -> int = {
return inspect arg -> int {
is "build" = subcommands::build(remaining_args);
is "clean" = subcommands::clean(remaining_args);
is "init" = subcommands::init(remaining_args);
is "run" = subcommands::run(remaining_args);
is _ = subcommands::unknown(arg);
};
Expand Down Expand Up @@ -496,16 +501,21 @@ warn_if_error: (context, p: fs::path, ec: std::error_code) -> void = {
}
}


find_root_dir: (dir: fs::path) -> std::optional<fs::path> = {
return find_root_dir(dir, "build.cpp2");
}

find_root_dir: (dir: fs::path, filename) -> std::optional<fs::path> = {
if dir.empty() { return std::nullopt; }

if fs::exists(dir / "build.cpp2") {
if fs::exists(dir / filename) {
return dir;
}

if !dir.has_parent_path() { return std::nullopt; }

return find_root_dir(dir.parent_path());
return find_root_dir(dir.parent_path(), filename);
}

build_binary_result: @struct type = {
Expand Down Expand Up @@ -905,7 +915,7 @@ subcommands: type = {
return build_result.exit_code;
}

log_info("binary built {} ({}ms)", fs::relative(build_result.outpath).generic_string(), build_result.duration.count());
log_success("binary built {} ({}ms)", fs::relative(build_result.outpath).generic_string(), build_result.duration.count());
}

return 0;
Expand All @@ -926,6 +936,55 @@ subcommands: type = {
return 0;
}

init: (args) -> int = {
if fs::exists("build.cpp2") {
log_error("already initialized cpp2b project!");
return 1;
}

dirname := fs::current_path().filename().string();

ensure_dir("src");

main_cpp2: std::ofstream = ("src/main.cpp2", std::ios::binary);
main_cpp2 << "main: () = std::println(\"Hello, (dirname)$!\");\n";

build_cpp2: std::ofstream = ("build.cpp2", std::ios::binary);
build_cpp2 << "import cpp2b.build;\n\n";
build_cpp2 << "build: (inout b: cpp2b::build) -> void = {\n";
build_cpp2 << "\t// rename binary so instead of it being 'src/main' it will be '(dirname)$'\n";
build_cpp2 << "\tb.binary_name(\"src/main\", \"(dirname)$\");\n";
build_cpp2 << "}\n";

main_cpp2.flush();
main_cpp2.close();
build_cpp2.flush();
build_cpp2.close();

log_info("added build.cpp2");
log_info("added src/main.cpp2");

if !fs::exists(".gitignore") {
if !find_root_dir(fs::current_path(), ".git") {
log_warning("assuming git repository");
}

gitignore: std::ofstream = (".gitignore", std::ios::binary);
gitignore << "# cpp2b cache directory\n";
gitignore << ".cache/cpp2\n";

gitignore.flush();
gitignore.close();

log_info("added .gitignore");
}

log_success("initialized cpp2b project '{}'", dirname);
log_info("run `cpp2b build` to build your project");

return 0;
}

unknown: (subcommand) -> int = {
log_error("unknown subcommand {}", subcommand);
return 1;
Expand Down

0 comments on commit 37c593b

Please sign in to comment.