This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.cr
More file actions
46 lines (38 loc) · 1.24 KB
/
Copy pathpackage.cr
File metadata and controls
46 lines (38 loc) · 1.24 KB
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
require "yaml"
require "regex"
class Package
property name : String
property author : String
property platform : String
property version : String?
property branch : String?
def initialize(@platform, @author, @name, @branch, @version); end
def path : String
"#{author}/#{name}"
end
def self.parse(uri : String) : Package
platforms = ["github", "gitlab", "bitbucket"].join("|")
regex = Regex.new("^(?:(#{platforms}):)?([^\/]+)\/([^#@]+)(?:#(\w+))?(?:@(.+))?$")
if info = regex.match(uri).not_nil!
platform = info[1]? || "github"
author = info[2]
name = info[3]
branch = info[4]?
version = info[5]?
return Package.new(platform, author, name, branch, version)
else
raise "Invalid package identifier."
end
end
def to_shard_h : Hash(YAML::Any, YAML::Any)
shard_h = Hash(String, String).new
shard_h[platform] = path
if !version.nil?
shard_h["version"] = version.not_nil!
end
if !branch.nil?
shard_h["branch"] = branch.not_nil!
end
shard_h.map { |k, v| [YAML::Any.new(k), YAML::Any.new(v)] }.to_h
end
end