-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshard.cr
105 lines (90 loc) · 2.69 KB
/
shard.cr
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
module Geode
class Dependency
include YAML::Serializable
property! name : String
property! version : String
property! path : String
property! git : String
property! github : String
property! gitlab : String
property! bitbucket : String
property! hg : String
property! fossil : String
def to_s(io : IO) : Nil
io << name
end
end
class Shard
include YAML::Serializable
NAME_REGEX = /\A[a-z][a-z0-9_-]+\z/
class Error < Exception
enum Code
NotFound
ParseException
end
getter code : Code
def initialize(@code, @message = nil)
end
end
property name : String
property description : String?
property authors : Array(String)?
property version : String
property crystal : String?
property documentation : String?
property license : String?
property repository : String?
property dependencies : Hash(String, Dependency) = {} of String => Dependency
@[YAML::Field(key: "development_dependencies")]
property development : Hash(String, Dependency) = {} of String => Dependency
property libraries : Hash(String, String) = {} of String => String
property executables : Array(String) = [] of String
property scripts : Hash(String, String) = {} of String => String
property targets : Hash(String, Hash(String, String)) = Hash(String, Hash(String, String)).new
getter dependency_shards : Hash(String, Shard) do
@dependencies.keys.map do |name|
{name, Shard.load(name)}
end.to_h
end
getter development_shards : Hash(String, Shard) do
@development.keys.map do |name|
{name, Shard.load(name)}
end.to_h
end
def self.load(source : String) : self
if source.ends_with? ".yml"
from_yaml File.read source
else
from_yaml File.read Path["lib", source, "shard.yml"].expand
end
rescue File::NotFoundError
raise Error.new :not_found
rescue ex : YAML::ParseException
raise Error.new :parse_exception, ex.to_s
end
def self.load_local : self
load "shard.yml"
end
def self.exists?(source : String) : Bool
if source.ends_with? ".yml"
File.exists? source
else
File.exists? Path["lib", source, "shard.yml"].expand
end
end
def has_postinstall? : Bool
@scripts.keys.any? &.starts_with? "postinstall"
end
def find_target_script(name : String, target : String) : String?
if @scripts.keys.any? &.starts_with? "#{name}@"
if script = @scripts["#{name}@#{target}"]?
script
else
@scripts[name]?
end
else
@scripts[name]?
end
end
end
end