|
1 | 1 | require "json_mapping" |
| 2 | +require "option_parser" |
2 | 3 | require "uri" |
3 | 4 | require "yaml_mapping" |
| 5 | +require "version_from_shard" |
4 | 6 |
|
5 | | -module Crystal2Nix |
6 | | - class PrefetchJSON |
7 | | - JSON.mapping(sha256: String) |
8 | | - end |
9 | | - |
10 | | - class ShardLock |
11 | | - YAML.mapping( |
12 | | - version: Float32, |
13 | | - shards: Hash(String, Hash(String, String)) |
14 | | - ) |
15 | | - end |
16 | | - |
17 | | - class RepoUrl |
18 | | - @url : URI |
19 | | - @path : Array(String) |
20 | | - |
21 | | - def initialize(kind : String, repo : String) |
22 | | - t = case kind |
23 | | - when "git" |
24 | | - repo |
25 | | - when "github" |
26 | | - "https://github.com/#{repo}" |
27 | | - else |
28 | | - ArgumentError.new "Unknown key: #{kind}" |
29 | | - exit 1 |
30 | | - end |
31 | | - @url = URI.parse(t).normalize |
32 | | - @path = @url.path.split("/") |
33 | | - end |
34 | | - |
35 | | - def github? |
36 | | - @url.host == "github.com" |
37 | | - end |
38 | | - |
39 | | - def owner : String |
40 | | - @path[1] |
41 | | - end |
42 | | - |
43 | | - def repo : String |
44 | | - github? ? @path[2].gsub(/\.git$/, "") : @path[2] |
45 | | - end |
| 7 | +require "./data" |
| 8 | +require "./repo" |
| 9 | +require "./worker" |
46 | 10 |
|
47 | | - def to_s : String |
48 | | - @url.to_s |
49 | | - end |
50 | | - end |
51 | | - |
52 | | - class Cli |
53 | | - SHARD_LOCK = "shard.lock" |
54 | | - SHARDS_NIX = "shards.nix" |
55 | | - SUPPORTED_KEYS = %w[git github] |
56 | | - |
57 | | - @lock_file : String |
58 | | - |
59 | | - def initialize |
60 | | - @lock_file = ARGV.fetch(1, SHARD_LOCK) |
61 | | - unless File.exists? @lock_file |
62 | | - STDERR.puts "ERROR: #{@lock_file} not found" |
63 | | - exit 1 |
64 | | - end |
65 | | - end |
66 | | - |
67 | | - def run |
68 | | - File.open SHARDS_NIX, "w+" do |file| |
69 | | - file.puts %({) |
70 | | - ShardLock.from_yaml(File.read(@lock_file)).shards.each do |key, value| |
71 | | - url = nil |
72 | | - SUPPORTED_KEYS.each do |k| |
73 | | - url = RepoUrl.new(k, value[k]) if value.has_key?(k) |
74 | | - end |
75 | | - if url.nil? |
76 | | - STDERR.puts "Unable to parse repository entry" |
77 | | - exit 1 |
78 | | - end |
79 | | - rev = if value["version"]? |
80 | | - if value["version"] =~ /^(?<version>.+)\+git\.commit\.(?<rev>.+)$/ |
81 | | - $~["rev"] |
82 | | - else |
83 | | - "v#{value["version"]}" |
84 | | - end |
85 | | - else |
86 | | - value["commit"] |
87 | | - end |
88 | | - sha256 = "" |
89 | | - args = ["--url", url.to_s, "--rev", rev] |
90 | | - Process.run("nix-prefetch-git", args: args) do |x| |
91 | | - x.error.each_line { |e| puts e } |
92 | | - sha256 = PrefetchJSON.from_json(x.output).sha256 |
93 | | - end |
94 | | - |
95 | | - file.puts %( #{key} = {) |
96 | | - file.puts %( owner = "#{url.owner}";) |
97 | | - file.puts %( repo = "#{url.repo}";) |
98 | | - file.puts %( rev = "#{rev}";) |
99 | | - file.puts %( sha256 = "#{sha256}";) |
100 | | - file.puts %( };) |
101 | | - end |
102 | | - file.puts %(}) |
103 | | - end |
104 | | - end |
105 | | - end |
| 11 | +module Crystal2Nix |
| 12 | + VersionFromShard.declare |
106 | 13 | end |
107 | | - |
108 | | -Crystal2Nix::Cli.new.run |
0 commit comments