Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
tensorush committed Mar 6, 2024
0 parents commit ddfa81a
Show file tree
Hide file tree
Showing 24 changed files with 956 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.zig text eol=lf
*.zon text eol=lf
42 changes: 42 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Continuous Integration

on:
push:
branches: [main]
paths: ["**.zig"]

pull_request:
branches: [main]
paths: ["**.zig"]

workflow_dispatch:

jobs:
exe:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Zig
uses: goto-bus-stop/setup-zig@v2

- name: Run executable
run: |
zig build exe
rm -rf liza/
zig build exe -- -l
lints:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Zig
uses: goto-bus-stop/setup-zig@v2

- name: Run lints
run: zig build lints
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
zig-cache/
zig-out/
liza/
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Jora Troosh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# :lizard: :star: liza

[![CI][ci-shd]][ci-url]
[![LC][lc-shd]][lc-url]

## Zig codebase initializer.

### :rocket: Usage

```sh
git clone https://github.com/tensorush/liza.git
cd liza/
zig build exe -- -h
```

### :sparkles: Features

- #### [Continuous integration GitHub workflow](src/templates/.github/workflows/ci.yaml):
- `?` (`exe` or `examples`): executable's run or library's example suite execution.
- `tests`: Test suite execution and code coverage publication to Codecov.
- `lints`: Linting checks.

- #### [Continuous delivery GitHub workflow](src/templates/.github/workflows/cd.yaml):
- Docs emission and deployment to GitHub Pages.

- #### [Zig executable template](src/templates/exe/):
- Dependency package usage.
- Custom GitHub README.
- `build.zig.zon`.

- #### [Zig library template](src/templates/lib/):
- Examples' directory setup.
- Custom GitHub README.
- `build.zig.zon`.

- #### [MIT license template](src/templates/LICENSE.md).

- #### [`.gitattributes`](src/templates/.gitattributes).

- #### [`.gitignore`](src/templates/.gitignore).

<!-- MARKDOWN LINKS -->

[ci-shd]: https://img.shields.io/github/actions/workflow/status/tensorush/liza/ci.yaml?branch=main&style=for-the-badge&logo=github&label=CI&labelColor=black
[ci-url]: https://github.com/tensorush/liza/blob/main/.github/workflows/ci.yaml
[lc-shd]: https://img.shields.io/github/license/tensorush/liza.svg?style=for-the-badge&labelColor=black
[lc-url]: https://github.com/tensorush/liza/blob/main/LICENSE.md
45 changes: 45 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const root_source_file = std.Build.LazyPath.relative("src/main.zig");
const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 0 };

// Dependencies
const clap_dep = b.dependency("clap", .{
.target = target,
.optimize = optimize,
});
const clap_mod = clap_dep.module("clap");

// Executable
const exe_step = b.step("exe", "Run executable");

const exe = b.addExecutable(.{
.name = "liza",
.target = target,
.version = version,
.optimize = optimize,
.root_source_file = root_source_file,
});
exe.root_module.addImport("clap", clap_mod);
b.installArtifact(exe);

const exe_run = b.addRunArtifact(exe);
if (b.args) |args| {
exe_run.addArgs(args);
}
exe_step.dependOn(&exe_run.step);
b.default_step.dependOn(exe_step);

// Lints
const lints_step = b.step("lints", "Run lints");

const lints = b.addFmt(.{
.paths = &.{ "src", "build.zig" },
.check = true,
});
lints_step.dependOn(&lints.step);
b.default_step.dependOn(lints_step);
}
17 changes: 17 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.{
.name = "liza",
.version = "0.1.0",
.dependencies = .{
.clap = .{
.url = "https://github.com/Hejsil/zig-clap/archive/0f2db77.tar.gz",
.hash = "12203896de6eedec14712f4f1eaac8b646939cfed213c56accf231a0abb05f9dbb77",
},
},
.paths = .{
"src/",
"build.zig",
"README.md",
"LICENSE.md",
"build.zig.zon",
},
}
173 changes: 173 additions & 0 deletions src/liza.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
const std = @import("std");

// Common paths.
const LICENSE_PATH = "LICENSE.md";
const CD_WORKFLOW_PATH = "cd.yaml";
const CI_WORKFLOW_PATH = "ci.yaml";
const GITIGNORE_PATH = ".gitignore";
const TEMPLATES_PATH = "templates/";
const GITATTRIBUTES_PATH = ".gitattributes";
const WORKFLOWS_PATH = ".github/workflows/";

// Custom paths.
const SRC_PATH = "src/";
const EXE_PATH = "exe/";
const LIB_PATH = "lib/";
const LIB_SRC_PATH = "lib.zig";
const EXE_SRC_PATH = "main.zig";
const README_PATH = "README.md";
const EXAMPLES_PATH = "examples/";
const BUILD_ZIG_PATH = "build.zig";
const BUILD_ZIG_ZON_PATH = "build.zig.zon";
const EXAMPLE1_PATH = EXAMPLES_PATH ++ "example1/";
const EXAMPLE2_PATH = EXAMPLES_PATH ++ "example2/";

// Common templates.
const LICENSE = @embedFile(TEMPLATES_PATH ++ LICENSE_PATH);
const GITIGNORE = @embedFile(TEMPLATES_PATH ++ GITIGNORE_PATH);
const GITATTRIBUTES = @embedFile(TEMPLATES_PATH ++ GITATTRIBUTES_PATH);
const CD_WORKFLOW = @embedFile(TEMPLATES_PATH ++ WORKFLOWS_PATH ++ CD_WORKFLOW_PATH);
const CI_WORKFLOW = @embedFile(TEMPLATES_PATH ++ WORKFLOWS_PATH ++ CI_WORKFLOW_PATH);

// Executable templates.
const EXE_README = @embedFile(TEMPLATES_PATH ++ EXE_PATH ++ README_PATH);
const EXE_BUILD_ZIG = @embedFile(TEMPLATES_PATH ++ EXE_PATH ++ BUILD_ZIG_PATH);
const EXE_SRC = @embedFile(TEMPLATES_PATH ++ EXE_PATH ++ SRC_PATH ++ EXE_SRC_PATH);
const EXE_BUILD_ZIG_ZON = @embedFile(TEMPLATES_PATH ++ EXE_PATH ++ BUILD_ZIG_ZON_PATH);

// Library templates.
const LIB_README = @embedFile(TEMPLATES_PATH ++ LIB_PATH ++ README_PATH);
const LIB_BUILD_ZIG = @embedFile(TEMPLATES_PATH ++ LIB_PATH ++ BUILD_ZIG_PATH);
const LIB_SRC = @embedFile(TEMPLATES_PATH ++ LIB_PATH ++ SRC_PATH ++ LIB_SRC_PATH);
const LIB_BUILD_ZIG_ZON = @embedFile(TEMPLATES_PATH ++ LIB_PATH ++ BUILD_ZIG_ZON_PATH);
const LIB_EXAMPLE1 = @embedFile(TEMPLATES_PATH ++ LIB_PATH ++ EXAMPLE1_PATH ++ EXE_SRC_PATH);
const LIB_EXAMPLE2 = @embedFile(TEMPLATES_PATH ++ LIB_PATH ++ EXAMPLE2_PATH ++ EXE_SRC_PATH);

pub fn initialize(
codebase_title: []const u8,
codebase_desc: []const u8,
user_handle: []const u8,
user_name: []const u8,
is_lib: bool,
) !void {
var codebase_dir = blk: {
const cur_dir = std.fs.cwd();
_ = cur_dir.openDir(codebase_title, .{}) catch break :blk try cur_dir.makeOpenPath(codebase_title, .{});
@panic("Codebase directory already exists!");
};
defer codebase_dir.close();

var workflows_dir = try codebase_dir.makeOpenPath(WORKFLOWS_PATH, .{});
defer workflows_dir.close();

var src_dir = try codebase_dir.makeOpenPath(SRC_PATH, .{});
defer src_dir.close();

try createPlain(GITIGNORE_PATH, GITIGNORE, codebase_dir);
try createPlain(CD_WORKFLOW_PATH, CD_WORKFLOW, workflows_dir);
try createPlain(GITATTRIBUTES_PATH, GITATTRIBUTES, codebase_dir);
try createLicense(LICENSE_PATH, LICENSE, user_name, codebase_dir);

if (is_lib) {
var example1_dir = try codebase_dir.makeOpenPath(EXAMPLE1_PATH, .{});
defer example1_dir.close();

var example2_dir = try codebase_dir.makeOpenPath(EXAMPLE2_PATH, .{});
defer example2_dir.close();

try createPlain(LIB_SRC_PATH, LIB_SRC, src_dir);
try createPlain(EXE_SRC_PATH, LIB_EXAMPLE1, example1_dir);
try createPlain(EXE_SRC_PATH, LIB_EXAMPLE2, example2_dir);
try createCi(EXAMPLES_PATH[0 .. EXAMPLES_PATH.len - 1], workflows_dir);
try createBuild(BUILD_ZIG_PATH, LIB_BUILD_ZIG, codebase_title, codebase_dir);
try createBuild(BUILD_ZIG_ZON_PATH, LIB_BUILD_ZIG_ZON, codebase_title, codebase_dir);
try createReadme(LIB_README, codebase_title, codebase_desc, user_handle, codebase_dir);
} else {
try createPlain(EXE_SRC_PATH, EXE_SRC, src_dir);
try createCi(EXE_PATH[0 .. EXE_PATH.len - 1], workflows_dir);
try createBuild(BUILD_ZIG_PATH, EXE_BUILD_ZIG, codebase_title, codebase_dir);
try createBuild(BUILD_ZIG_ZON_PATH, EXE_BUILD_ZIG_ZON, codebase_title, codebase_dir);
try createReadme(EXE_README, codebase_title, codebase_desc, user_handle, codebase_dir);
}
}

fn createReadme(
comptime README: []const u8,
codebase_title: []const u8,
codebase_desc: []const u8,
user_handle: []const u8,
codebase_dir: std.fs.Dir,
) !void {
var readme_file = try codebase_dir.createFile(README_PATH, .{});
defer readme_file.close();

var idx: usize = 0;
while (std.mem.indexOfScalar(u8, README[idx..], '?')) |i| : (idx += i + 2) {
try readme_file.writeAll(README[idx .. idx + i]);
switch (README[idx + i + 1]) {
't' => try readme_file.writeAll(codebase_title),
'd' => try readme_file.writeAll(codebase_desc),
'h' => try readme_file.writeAll(user_handle),
else => try readme_file.writeAll(README[idx + i .. idx + i + 2]),
}
}
try readme_file.writeAll(README[idx..]);
}

fn createBuild(
comptime PATH: []const u8,
comptime TEXT: []const u8,
codebase_title: []const u8,
codebase_dir: std.fs.Dir,
) !void {
var build_file = try codebase_dir.createFile(PATH, .{});
defer build_file.close();

var idx: usize = 0;
while (std.mem.indexOfScalar(u8, TEXT[idx..], '?')) |i| : (idx += i + 1) {
try build_file.writeAll(TEXT[idx .. idx + i]);
try build_file.writeAll(codebase_title);
}
try build_file.writeAll(TEXT[idx..]);
}

fn createCi(
comptime STEP: []const u8,
workflows_dir: std.fs.Dir,
) !void {
var ci_file = try workflows_dir.createFile(CI_WORKFLOW_PATH, .{});
defer ci_file.close();

var idx: usize = 0;
while (std.mem.indexOfScalar(u8, CI_WORKFLOW[idx..], '?')) |i| : (idx += i + 1) {
try ci_file.writeAll(CI_WORKFLOW[idx .. idx + i]);
try ci_file.writeAll(STEP);
}
try ci_file.writeAll(CI_WORKFLOW[idx..]);
}

fn createLicense(
comptime PATH: []const u8,
comptime TEXT: []const u8,
user_name: []const u8,
codebase_dir: std.fs.Dir,
) !void {
var license_file = try codebase_dir.createFile(PATH, .{});
defer license_file.close();

const idx = std.mem.indexOfScalar(u8, TEXT, '?').?;
try license_file.writeAll(TEXT[0..idx]);
try license_file.writeAll(user_name);
try license_file.writeAll(TEXT[idx + 1 ..]);
}

fn createPlain(
comptime PATH: []const u8,
comptime TEXT: []const u8,
dir: std.fs.Dir,
) !void {
var plain_file = try dir.createFile(PATH, .{});
defer plain_file.close();

try plain_file.writeAll(TEXT);
}
Loading

0 comments on commit ddfa81a

Please sign in to comment.