File: cli.cr

package info (click to toggle)
shards 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 904 kB
  • sloc: makefile: 117
file content (119 lines) | stat: -rw-r--r-- 2,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Spec.before_each do
  path = application_path

  if File.exists?(path)
    Shards::Helpers.rm_rf_children(path)
  else
    Dir.mkdir_p(path)
  end
end

def with_shard(metadata, lock = nil, override = nil, &)
  Dir.cd(application_path) do
    File.write "shard.yml", to_shard_yaml(metadata)
    File.write "shard.lock", to_lock_yaml(lock) if lock
    File.write "shard.override.yml", to_override_yaml(override) if override
    yield
  end
end

def to_shard_yaml(metadata)
  String.build do |yml|
    yml << "name: " << (metadata[:name]? || "test").inspect << '\n'
    yml << "version: " << (metadata[:version]? || "0.0.0").inspect << '\n'

    metadata.each do |key, value|
      if key.to_s.ends_with?("dependencies")
        write_dependencies(yml, key, value)
      elsif key.to_s == "targets"
        yml << "targets:\n"
        if value.responds_to?(:each)
          value.each do |target, info|
            yml << "  " << target.to_s << ":\n"
            if info.responds_to?(:each)
              info.each do |main, src|
                yml << "    main: " << src.inspect << '\n'
              end
            end
          end
        end
      end
    end
  end
end

def to_override_yaml(metadata)
  String.build do |yml|
    metadata.each do |key, value|
      if key.to_s == "dependencies"
        write_dependencies(yml, key, value)
      end
    end
  end
end

# This is used for dependencies and development_dependencies
private def write_dependencies(yml, key, value)
  yml << key << ':'

  if value.responds_to?(:each)
    yml << '\n'
    value.each do |name, version|
      yml << "  " << name << ":\n"

      case version
      when String
        yml << "    git: " << git_url(name).inspect << '\n'
        yml << "    version: " << version.inspect << '\n' unless version == "*"
        # when Hash
        #  version.each do |k, v|
        #    yml << "    " << k << ": " << v.inspect << '\n'
        #  end
      when NamedTuple
        version.each do |k, v|
          yml << "    " << k.to_s << ": " << v.inspect << '\n'
        end
      else
        yml << "    git: " << git_url(name).inspect << '\n'
      end
    end
  else
    yml << value
  end
end

def to_lock_yaml(lock)
  return unless lock

  YAML.dump({
    version: Shards::Lock::CURRENT_VERSION,
    shards:  lock.to_a.to_h do |name, data|
      if data.is_a?(NamedTuple)
        git = data[:git]
        version = data[:version]
      else
        git = git_url(name)
        version = data
      end
      {name, {git: git, version: version}}
    end,
  })
end

module Shards::Specs
  @@application_path : String?

  def self.application_path
    @@application_path ||= File.expand_path("../../tmp/integration", __DIR__).tap do |path|
      if File.exists?(path)
        Shards::Helpers.rm_rf_children(path)
      else
        Dir.mkdir_p(path)
      end
    end
  end
end

def application_path
  Shards::Specs.application_path
end