Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(back): #997 make ruby lock #998

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ Real life projects that run entirely on [Makes][makes]:
- [Utilities](#utilities)
- [makeNodeJsLock](#makenodejslock)
- [makePythonLock](#makepythonlock)
- [makeRubyLock](#makerubylock)
- [makeSopsEncryptedFile](#makesopsencryptedfile)
- [workspaceForTerraformFromEnv](#workspaceforterraformfromenv)
- [Framework Configuration](#framework-configuration)
Expand Down Expand Up @@ -604,7 +605,7 @@ for instance:
makesSrc = builtins.fetchGit {
url = "https://github.com/fluidattacks/makes";
ref = "refs/tags/22.11";
rev = "" # Add a commit here
rev = ""; # Add a commit here
};
}
```
Expand Down Expand Up @@ -2663,6 +2664,33 @@ Django: "3.2.*"
psycopg2: "2.9.1"
```

- `sources_yaml` is the **absolute path**
to a file were the script will output results.

### makeRubyLock

You can generate a `sourcesYaml`
for [makeRubyGemsEnvironment](#makerubygemsenvironment)
like this:

```bash
m github:fluidattacks/[email protected] /utils/makeRubyLock \
"${ruby_version}" \
"${dependencies_yaml}" \
"${sources_yaml}"
```

- Supported `ruby_version`s are: `2.7`, `3.0` and `3.1`.
- `dependencies_yaml` is the **absolute path** to a [YAML][yaml] file
mapping [RubyGems][rubygems] gems to version constraints.

Example:

```yaml
rubocop: "1.43.0"
slim: "~> 4.1"
```

- `sources_yaml` is the **absolute path**
to a file were the script will output results.

Expand Down Expand Up @@ -4370,7 +4398,7 @@ Types:

- makeRubyVersion (`function str -> package`):

- (`enum [ "2.6" "2.7" "3.0" ]`):
- (`enum [ "2.7" "3.0" "3.1" ]`):
Version of the [Ruby][ruby] interpreter.

Example:
Expand All @@ -4387,7 +4415,7 @@ makeScript {
'';
name = "example";
searchPaths = {
bin = [ (makeRubyVersion "2.6") ];
bin = [ (makeRubyVersion "2.7") ];
};
}
```
Expand Down Expand Up @@ -5332,7 +5360,7 @@ let
makes = import "${builtins.fetchGit {
url = "https://github.com/fluidattacks/makes";
ref = "refs/tags/22.11";
rev = "" # Add a commit here
rev = ""; # Add a commit here
}}/src/args/agnostic.nix" { };
in
# Use the framework
Expand Down
29 changes: 29 additions & 0 deletions makes/utils/makeRubyLock/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# shellcheck shell=bash

function main {
local ruby_version="${1}"
local deps_path="${2}"
local lock_path="${3}"
local ruby

: \
&& case "${ruby_version}" in
2.7) ruby=__argRuby27__ ;;
3.0) ruby=__argRuby30__ ;;
3.1) ruby=__argRuby31__ ;;
*) critical Ruby version not supported: "${ruby_version}" ;;
esac \
&& info "Generating manifest:" \
&& pushd "$(mktemp -d)" \
&& "__argRuby27__/bin/ruby" \
"__argParser__" \
"${ruby_version}" \
"${ruby}" \
"${deps_path}" \
> sources.json \
&& yj -yy < sources.json > "${lock_path}" \
&& popd \
&& info "Generated a lock file at ${lock_path}"
}

main "${@}"
20 changes: 20 additions & 0 deletions makes/utils/makeRubyLock/main.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
__nixpkgs__,
makeRubyVersion,
makeScript,
...
}:
makeScript {
entrypoint = ./entrypoint.sh;
name = "make-ruby-lock";
replace = {
__argParser__ = ./parser.rb;
__argRuby27__ = makeRubyVersion "2.7";
__argRuby30__ = makeRubyVersion "3.0";
__argRuby31__ = makeRubyVersion "3.1";
};
searchPaths.bin = [
__nixpkgs__.nix
__nixpkgs__.yj
];
}
63 changes: 63 additions & 0 deletions makes/utils/makeRubyLock/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require 'bundler'
require 'open3'
require 'yaml'

def get_link(name, version)
file = "#{name}-#{version}.gem"
url = "https://rubygems.org/downloads/#{file}"
{
'name' => file,
'sha256' => Open3.capture3(
'nix-prefetch-url',
url
)[0].strip,
'url' => url
}
end

def generate_gemfile(deps_path)
deps = YAML.load_file(deps_path)
gemfile = "source 'https://rubygems.org'\n"

deps.each do |dep, version|
gemfile += "gem '#{dep}', '#{version}'\n"
end

File.write('Gemfile', gemfile)
end

def generate_lock(ruby_path)
Open3.capture3(
"#{ruby_path}/bin/bundle",
'lock'
)
end

def generate_sources(ruby_version)
lock = Bundler::LockfileParser.new(
Bundler.read_file(Bundler.default_lockfile)
)

result = {
'closure' => {},
'links' => [],
'ruby' => ruby_version
}

lock.specs.each do |dep|
result['closure'][dep.name] = dep.version.to_s
result['links'].append(get_link(dep.name, dep.version))
end

result.to_yaml
end

def main(ruby_version, ruby_path, deps_path)
generate_gemfile(deps_path)
generate_lock(ruby_path)
generate_sources(ruby_version)
end

puts(main(ARGV[0], ARGV[1], ARGV[2]))
1 change: 1 addition & 0 deletions src/args/make-ruby-version/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
{
"2.7" = __nixpkgs__.ruby_2_7;
"3.0" = __nixpkgs__.ruby_3_0;
"3.1" = __nixpkgs__.ruby_3_1;
}
.${version}