Skip to content

Commit

Permalink
huge refactoring into separate packages
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Oct 11, 2024
1 parent 6205086 commit b359e25
Show file tree
Hide file tree
Showing 246 changed files with 3,422 additions and 2,629 deletions.
2 changes: 0 additions & 2 deletions .crystalline_main.cr

This file was deleted.

21 changes: 16 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
macos_x86_64:
runs-on: macos-latest
runs-on: macos-13
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -31,7 +31,10 @@ jobs:
cp $(brew --prefix)/opt/pcre2/lib/libpcre2-8.a ./libs
- name: Build the binary
# Statically link most non-system libraries
run: env CRYSTAL_LIBRARY_PATH=`pwd`/libs shards build --no-debug --release -Dpreview_mt
run: |
env CRYSTAL_LIBRARY_PATH=`pwd`/libs crystal projects.cr build:cli --no-debug --release -Dpreview_mt
mkdir bin
mv ./packages/cli/bin/zap ./bin/zap
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
with:
Expand Down Expand Up @@ -61,7 +64,10 @@ jobs:
# libpcre
cp $(brew --prefix)/opt/pcre2/lib/libpcre2-8.a ./libs
- name: Build the binary
run: env CRYSTAL_LIBRARY_PATH=`pwd`/libs shards build --no-debug --release -Dpreview_mt
run: |
env CRYSTAL_LIBRARY_PATH=`pwd`/libs crystal projects.cr build:cli --no-debug --release -Dpreview_mt
mkdir bin
mv ./packages/cli/bin/zap ./bin/zap
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
with:
Expand All @@ -76,7 +82,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2
- name: Build the static binary
run: shards build --production --release --static --no-debug --stats
run: |
crystal projects.cr build:cli --production --release --static --no-debug --stats
mkdir bin
mv ./packages/cli/bin/zap ./bin/zap
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
with:
Expand All @@ -93,7 +102,9 @@ jobs:
- name: Build the binary
shell: bash
run: |
shards build --progress --release --no-debug --stats
crystal projects.cr build:cli --progress --release --no-debug --stats
mkdir bin
mv ./packages/cli/bin/zap ./bin/zap
ls -al ./bin
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ jobs:
- name: Install Crystal
uses: crystal-lang/install-crystal@v1
- name: Install dependencies
run: shards install
run: crystal projects.cr install
- name: Run tests
run: crystal spec
run: crystal projects.cr spec
- name: Run tests (multithreaded)
run: crystal spec -Dpreview_mt
run: crystal projects.cr spec -Dpreview_mt
launch:
name: Build and check if the binary is working
strategy:
Expand All @@ -33,8 +33,8 @@ jobs:
- name: Install Crystal
uses: crystal-lang/install-crystal@v1
- name: Install dependencies
run: shards install
run: crystal projects.cr install
- name: Build
run: shards build
run: crystal projects.cr build:cli
- name: Print version
run: ./bin/zap --version
run: ./packages/cli/bin/zap --version
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/docs/
/lib/
/bin/
lib/
bin/
/.shards/
*.dwarf
node_modules
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ On top of that, zap will also try to cache package manifests in order to avoid u

```bash
git clone https://github.com/elbywan/zap
shards install
crystal projects.cr spec install
# Run the specs
crystal spec
crystal projects.cr spec
# Build locally (-Dpreview_mt might not work on some os/arch)
shards build --progress -Dpreview_mt --release
crystal projects.cr -p cli build --progress -Dpreview_mt --release
# Run the binary
./packages/cli/bin/zap --help
```

## Contributing
Expand Down
88 changes: 60 additions & 28 deletions src/backend/backend.cr → packages/backend/backend.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "file_utils"
require "../utils/concurrent/pipeline"
require "concurrency/pipeline"
require "extensions/dir"

module Zap::Backend
alias Pipeline = ::Zap::Utils::Concurrent::Pipeline
module Backend
alias Pipeline = Concurrency::Pipeline

enum Backends
CloneFile
Expand All @@ -12,29 +13,6 @@ module Zap::Backend
Symlink
end

def self.install(*, dependency : Package, target : Path | String, backend : Backends, store : Store, &on_installing) : Bool
case backend
in .clone_file?
{% if flag?(:darwin) %}
Backend::CloneFile.install(dependency, target, store: store, &on_installing)
{% else %}
raise "The clonefile backend is not supported on this platform"
{% end %}
in .copy_file?
{% if flag?(:darwin) %}
Backend::CopyFile.install(dependency, target, store: store, &on_installing)
{% else %}
raise "The copyfile backend is not supported on this platform"
{% end %}
in .hardlink?
Backend::Hardlink.install(dependency, target, store: store, &on_installing)
in .copy?
Backend::Copy.install(dependency, target, store: store, &on_installing)
in .symlink?
Backend::Symlink.install(dependency, target, store: store, &on_installing)
end
end

# -----------------
# Iterative version
# -----------------
Expand Down Expand Up @@ -119,10 +97,64 @@ module Zap::Backend
# end
# end

protected def self.prepare(dependency : Package, dest_path : Path | String, *, store : Store, mkdir_parent = false) : {Path, Path, Bool}
protected def self.prepare(dependency : Data::Package, dest_path : Path | String, *, store : Store, mkdir_parent = false) : {Path, Path, Bool}
src_path = store.package_path(dependency)
already_installed = Installer.package_already_installed?(dependency, dest_path)
already_installed = self.package_already_installed?(dependency.key, dest_path)
Utils::Directories.mkdir_p(mkdir_parent ? dest_path.dirname : dest_path) unless already_installed
{src_path, dest_path, already_installed}
end

# Check if a package is already installed on the filesystem
def self.package_already_installed?(package_key : String, path : Path)
if exists = Dir.exists?(path)
metadata_path = path / Shared::Constants::METADATA_FILE_NAME
unless File.readable?(metadata_path)
FileUtils.rm_rf(path)
exists = false
else
key = File.read(metadata_path)
if key != package_key
FileUtils.rm_rf(path)
exists = false
end
end
end
exists
end
end

require "./clonefile"
require "./copy"
require "./copyfile"
require "./hardlink"
require "./symlink"

module Backend
def self.install(*, dependency : Data::Package, target : Path | String, backend : Backends, store : Store, &on_installing) : Bool
src_path, dest_path, already_installed = self.prepare(dependency, target, store: store)
return false if already_installed

yield

case backend
in .clone_file?
{% if flag?(:darwin) %}
Backend::CloneFile.install(src_path, dest_path)
{% else %}
raise "The clonefile backend is not supported on this platform"
{% end %}
in .copy_file?
{% if flag?(:darwin) %}
Backend::CopyFile.install(src_path, dest_path)
{% else %}
raise "The copyfile backend is not supported on this platform"
{% end %}
in .hardlink?
Backend::Hardlink.install(src_path, dest_path)
in .copy?
Backend::Copy.install(src_path, dest_path)
in .symlink?
Backend::Symlink.install(src_path, dest_path)
end
end
end
11 changes: 11 additions & 0 deletions packages/backend/clonefile.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "extensions/libc/clonefile"

module Backend::CloneFile
def self.install(src_path : String | Path, dest_path : String | Path) : Bool
result = LibC.clonefile(src_path.to_s, dest_path.to_s, 0)
if result == -1
raise "Error cloning file: #{Errno.value} #{src_path.to_s} -> #{dest_path.to_s}"
end
true
end
end
13 changes: 13 additions & 0 deletions packages/backend/copy.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "concurrency/pipeline"

module Backend::Copy
def self.install(src_path : String | Path, dest_path : String | Path) : Bool
# FileUtils.cp_r(src_path, dest_path)
Pipeline.wrap(force_wait: true) do |pipeline|
Backend.recursively(src_path.to_s, dest_path.to_s, pipeline: pipeline) do |src, dest|
File.copy(src, dest)
end
end
true
end
end
13 changes: 13 additions & 0 deletions packages/backend/copyfile.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "extensions/libc/copyfile"
require "concurrency/pipeline"

module Backend::CopyFile
def self.install(src_path : String | Path, dest_path : String | Path) : Bool
Pipeline.wrap(force_wait: true) do |pipeline|
Backend.recursively(src_path.to_s, dest_path.to_s, pipeline) do |src, dest|
LibC.copyfile(src.to_s, dest.to_s, nil, LibC::COPYFILE_CLONE_FORCE | LibC::COPYFILE_ALL)
end
end
true
end
end
12 changes: 12 additions & 0 deletions packages/backend/hardlink.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "concurrency/pipeline"

module Backend::Hardlink
def self.install(src_path : String | Path, dest_path : String | Path) : Bool
Pipeline.wrap(force_wait: true) do |pipeline|
Backend.recursively(src_path.to_s, dest_path.to_s, pipeline: pipeline) do |src, dest|
File.link(src, dest)
end
end
true
end
end
38 changes: 38 additions & 0 deletions packages/backend/shard.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: 2.0
shards:
concurrency:
path: ../concurrency
version: 0.1.0

crystar:
git: https://github.com/naqvis/crystar.git
version: 0.4.0

data:
path: ../data
version: 0.1.0

extensions:
path: ../extensions
version: 0.1.0

git:
path: ../git
version: 0.1.0

msgpack:
git: https://github.com/crystal-community/msgpack-crystal.git
version: 1.3.4

semver:
path: ../semver
version: 0.1.0

shared:
path: ../shared
version: 0.1.0

utils:
path: ../utils
version: 0.1.0

12 changes: 12 additions & 0 deletions packages/backend/shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: backend
version: 0.1.0

dependencies:
concurrency:
path: ../concurrency
data:
path: ../data
shared:
path: ../shared
extensions:
path: ../extensions
12 changes: 12 additions & 0 deletions packages/backend/symlink.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "concurrency/pipeline"

module Backend::Symlink
def self.install(src_path : String | Path, dest_path : String | Path) : Bool
Pipeline.wrap do |pipeline|
Backend.recursively(src_path.to_s, dest_path.to_s, pipeline: pipeline) do |src, dest|
File.symlink(src, dest)
end
end
true
end
end
Binary file added packages/cli/bin/zap
Binary file not shown.
44 changes: 44 additions & 0 deletions packages/cli/cli.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "option_parser"
require "core/command_config"
require "core/config"
require "commands/cli"
require "commands/helpers"

class CLI
@command_config_ref = Core::CommandConfigRef.new

def initialize(
@commands : Array(Commands::CLI),
@config : Core::Config = Core::Config.new(ENV, "ZAP")
)
end

def parse
# Parse options and extract configs
parser = OptionParser.new do |parser|
banner_desc = <<-DESCRIPTION
#{"A package manager for the Javascript language.".colorize.bold}
Check out #{"https://github.com/elbywan/zap".colorize.magenta} for more information.
DESCRIPTION
Commands::Helpers.banner(parser, "[command]", banner_desc)

Commands::Helpers.separator("Commands")
@commands.each &.register(parser, @command_config_ref)

Commands::Helpers.separator("Options")
Commands::Helpers.common_options(true)
Commands::Helpers.workspace_options(true)
end

parser.parse

if @command_config_ref.ref.nil?
puts parser
exit
end

# Return both configs
{@config, @command_config_ref.ref}
end
end
Loading

0 comments on commit b359e25

Please sign in to comment.