capsium 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +1 -4
  3. data/.github/workflows/release.yml +1 -2
  4. data/.rubocop.yml +10 -6
  5. data/Gemfile +0 -6
  6. data/capsium.gemspec +9 -8
  7. data/lib/capsium/cli.rb +29 -10
  8. data/lib/capsium/converters/jekyll.rb +63 -30
  9. data/lib/capsium/package/dataset.rb +35 -48
  10. data/lib/capsium/package/dataset_config.rb +28 -0
  11. data/lib/capsium/package/manifest.rb +21 -41
  12. data/lib/capsium/package/manifest_config.rb +19 -0
  13. data/lib/capsium/package/metadata.rb +13 -27
  14. data/lib/capsium/package/metadata_config.rb +17 -0
  15. data/lib/capsium/package/routes.rb +39 -97
  16. data/lib/capsium/package/routes_config.rb +74 -0
  17. data/lib/capsium/package/storage.rb +47 -22
  18. data/lib/capsium/package/storage_config.rb +9 -0
  19. data/lib/capsium/package.rb +18 -6
  20. data/lib/capsium/packager.rb +25 -63
  21. data/lib/capsium/protector.rb +15 -7
  22. data/lib/capsium/reactor.rb +52 -28
  23. data/lib/capsium/version.rb +1 -1
  24. metadata +73 -25
  25. data/images/005df7415a331c466ad2d9a42efdd212b6f42fe50b5fd3b3174c86c706f58244.png +0 -0
  26. data/images/0374025b3af99b8a473282c8cbbf9fcd29573cf41e586982f328f86c0690f43d.png +0 -0
  27. data/images/0bb4da785be40ef58e219470ebccb979325928b75453dc46bac23c6ee8a7a7cb.png +0 -0
  28. data/images/6aa294dccc81af594aacbe804e7ddffdc17eacc28357338108aea5d021d831ff.png +0 -0
  29. data/images/72dd3fbf3f4b475e27a0e7fb8137c475c32c41f8d222bcf62d6a9ccf102d9532.png +0 -0
  30. data/images/8772b6961d169738d7b0fa0b669b06fc2f40632d4c62586c7634fc17b93182a3.png +0 -0
  31. data/images/a998d842405933d45723606ff3f70162ec95b4ef30db25464a366184fd08fb9b.png +0 -0
  32. data/images/aa8980547e8c003d33273ab4d80e62da7f317bd7581b293c06d67f5331f24f31.png +0 -0
  33. data/images/bb78a872b539e0e9b2d80dee58acbb688f3f2727b324a5bf8bf417a69d94a166.png +0 -0
  34. data/images/c48fc83b17725d85fbb64d971196ebfccd8c5c757fe6aa5845303f6e315879b6.png +0 -0
  35. data/images/f08ef07308d08119ac2124bb7428c8bef17ef1ca70045696604d6e83015a9b91.png +0 -0
  36. data/images/f7514206111b695647eae9adfcf498ba3e0ff83ecfe25f3fc3ed8e9f04c5c726-1.png +0 -0
  37. data/images/f7514206111b695647eae9adfcf498ba3e0ff83ecfe25f3fc3ed8e9f04c5c726.png +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e89423acb8bc50c9e1fc53ad06ad472114f379159cf66f49d90f92f08a22c907
4
- data.tar.gz: 70e5bcdcfcbf17028102da8c6dd45a4b9d5fe58a0618f048214f278fc212516b
3
+ metadata.gz: e2b954beb33b36d3300f8ff418f643a9afbbf688fad4e8333b195ccb76b38b41
4
+ data.tar.gz: b2ef9e049b70ce3eb111ef0c136befaaf7a2f6a4d233ed9fb41a9d27d9c39e2c
5
5
  SHA512:
6
- metadata.gz: 98aa164541a7fdb7678baaa084cad87bccb9f5673913a521d91ed19747153c119bccdbaeb7771c2312f131270d066412fcf1972c43cc7ac38dc4b3016e1f96b8
7
- data.tar.gz: e3d043c1c4c1692160811fbefdce727c2c0d606e65ad934de9474b41af88a3e621bd51b9620581f186dd257800e3386661f125ab94921277a4bd5a7b1e801a49
6
+ metadata.gz: e3c75b5ab47ef3b7fe5a8f20571a300c15b960951c032d9c8d01e3eff295a237c294288613449ed090c73e38df718986f412573deeafd0f33acd7a43ead76c41
7
+ data.tar.gz: 8bdb432b2b7efb4987d211608e8cb13828e2bf49d49cf921c3d9a7f7cc74f5a775ed6454e5bfd3e91a539a36fa0beb28395ff7910682d9d791f8c44db3eebe26
@@ -8,11 +8,8 @@ on:
8
8
  tags: [ v* ]
9
9
  pull_request:
10
10
 
11
- permissions:
12
- contents: write
13
-
14
11
  jobs:
15
12
  rake:
16
- uses: metanorma/ci/.github/workflows/libreoffice-rake.yml@main
13
+ uses: metanorma/ci/.github/workflows/generic-rake.yml@main
17
14
  secrets:
18
15
  pat_token: ${{ secrets.METANORMA_CI_PAT_TOKEN }}
@@ -7,8 +7,7 @@ on:
7
7
  inputs:
8
8
  next_version:
9
9
  description: |
10
- Next release version. Possible values: x.y.z, major, minor, patch (or pre|rc|etc).
11
- Also, you can pass 'skip' to skip 'git tag' and do 'gem push' for the current version
10
+ Next release version. Possible values: x.y.z, major, minor, patch or pre|rc|etc
12
11
  required: true
13
12
  default: 'skip'
14
13
  repository_dispatch:
data/.rubocop.yml CHANGED
@@ -1,8 +1,12 @@
1
- AllCops:
2
- TargetRubyVersion: 3.0
1
+ inherit_from:
2
+ - https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
3
3
 
4
- Style/StringLiterals:
5
- EnforcedStyle: double_quotes
4
+ # local repo-specific modifications
5
+ # ...
6
6
 
7
- Style/StringLiteralsInInterpolation:
8
- EnforcedStyle: double_quotes
7
+ AllCops:
8
+ TargetRubyVersion: 2.6
9
+ NewCops: enable
10
+ Exclude:
11
+ - 'lib/sts/mapper.rb'
12
+ - 'vendor/**/*'
data/Gemfile CHANGED
@@ -4,9 +4,3 @@ source "https://rubygems.org"
4
4
 
5
5
  # Specify your gem's dependencies in capsium.gemspec
6
6
  gemspec
7
-
8
- gem "rake", "~> 13.0"
9
-
10
- gem "rspec", "~> 3.0"
11
-
12
- gem "rubocop", "~> 1.21"
data/capsium.gemspec CHANGED
@@ -6,20 +6,17 @@ Gem::Specification.new do |spec|
6
6
  spec.name = "capsium"
7
7
  spec.version = Capsium::VERSION
8
8
  spec.authors = ["Ribose Inc."]
9
- spec.email = ["open.source@ribose.com'"]
9
+ spec.email = ["open.source@ribose.com"]
10
10
 
11
11
  spec.summary = "Capsium"
12
12
  spec.description = "Capsium"
13
- spec.homepage = "https://github.com/metanorma/capsium"
13
+ spec.homepage = "https://github.com/capsiums/capsium"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.metadata["homepage_uri"] = spec.homepage
17
17
  spec.metadata["source_code_uri"] = spec.homepage
18
- spec.metadata["changelog_uri"] = "https://github.com/metanorma/capsium/releases"
18
+ spec.metadata["changelog_uri"] = "https://github.com/capsiums/capsium/releases"
19
19
 
20
- # Specify which files should be added to the gem when it is released.
21
- # The `git ls-files -z` loads the files in the RubyGem
22
- # that have been added into git.
23
20
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
21
  `git ls-files -z`
25
22
  .split("\x0")
@@ -35,10 +32,12 @@ Gem::Specification.new do |spec|
35
32
  spec.add_dependency "csv"
36
33
  spec.add_dependency "htmlcompressor"
37
34
  spec.add_dependency "html-proofer"
35
+ spec.add_dependency "jekyll"
38
36
  spec.add_dependency "json"
37
+ spec.add_dependency "json-schema"
39
38
  spec.add_dependency "listen"
40
39
  spec.add_dependency "marcel"
41
- spec.add_dependency "rubyzip", "~>2.3.2"
40
+ spec.add_dependency "rubyzip", "~> 2.3.2"
42
41
  spec.add_dependency "scss_lint"
43
42
  spec.add_dependency "shale"
44
43
  spec.add_dependency "sqlite3"
@@ -47,8 +46,10 @@ Gem::Specification.new do |spec|
47
46
  spec.add_dependency "webrick"
48
47
  spec.add_dependency "yaml"
49
48
 
50
- spec.add_development_dependency "pry", "~> 0.12.2"
49
+ spec.add_development_dependency "pry"
50
+ spec.add_development_dependency "rack-test"
51
51
  spec.add_development_dependency "rake", "~> 13.0"
52
52
  spec.add_development_dependency "rspec", "~> 3.11"
53
53
  spec.add_development_dependency "rubocop", "~> 1.58"
54
+ spec.add_development_dependency "rubocop-performance"
54
55
  end
data/lib/capsium/cli.rb CHANGED
@@ -15,6 +15,7 @@ module Capsium
15
15
  extend ThorExt::Start
16
16
 
17
17
  desc "info PACKAGE_PATH", "Display information about the package"
18
+
18
19
  def info(path_to_package)
19
20
  package = Capsium::Package.new(path_to_package)
20
21
  puts "Package Path: #{package.path}"
@@ -22,25 +23,30 @@ module Capsium
22
23
  puts "Manifest: #{package.manifest.as_json}"
23
24
  end
24
25
 
25
- desc "manifest PATH_TO_PACKAGE", "Show the manifest content of the package"
26
+ desc "manifest PATH_TO_PACKAGE",
27
+ "Show the manifest content of the package"
28
+
26
29
  def manifest(path_to_package)
27
30
  package = Capsium::Package.new(path_to_package)
28
31
  puts JSON.pretty_generate(package.manifest.as_json)
29
32
  end
30
33
 
31
34
  desc "storage PATH_TO_PACKAGE", "Show the storage datasets of the package"
35
+
32
36
  def storage(path_to_package)
33
37
  package = Capsium::Package.new(path_to_package)
34
38
  puts JSON.pretty_generate(package.storage.as_json)
35
39
  end
36
40
 
37
41
  desc "routes PATH_TO_PACKAGE", "Show the routes of the package"
42
+
38
43
  def routes(path_to_package)
39
44
  package = Capsium::Package.new(path_to_package)
40
45
  puts JSON.pretty_generate(package.routes.as_json)
41
46
  end
42
47
 
43
48
  desc "metadata PATH_TO_PACKAGE", "Show the metadata of the package"
49
+
44
50
  def metadata(path_to_package)
45
51
  package = Capsium::Package.new(path_to_package)
46
52
  puts JSON.pretty_generate(package.metadata.as_json)
@@ -48,6 +54,7 @@ module Capsium
48
54
 
49
55
  desc "pack PATH_TO_PACKAGE_FOLDER", "Package the files into the package"
50
56
  option :force, type: :boolean, default: false, aliases: "-f"
57
+
51
58
  def pack(path_to_package)
52
59
  package = Capsium::Package.new(path_to_package)
53
60
  packager = Capsium::Packager.new
@@ -61,25 +68,37 @@ module Capsium
61
68
 
62
69
  class Reactor < Thor
63
70
  extend ThorExt::Start
64
- desc "serve PACKAGE_PATH", "Start the Capsium reactor to serve the package"
71
+
72
+ desc "serve PACKAGE_PATH",
73
+ "Start the Capsium reactor to serve the package"
74
+ option :port, type: :numeric, default: Capsium::Reactor::DEFAULT_PORT
75
+ option :do_not_listen, type: :boolean, default: false
76
+
65
77
  def serve(path_to_package)
66
- package = Capsium::Package.new(path_to_package)
67
- reactor = Capsium::Reactor.new(package)
78
+ reactor = Capsium::Reactor.new(
79
+ package: path_to_package,
80
+ port: options[:port],
81
+ do_not_listen: options[:do_not_listen],
82
+ )
68
83
  reactor.serve
69
84
  rescue StandardError => e
70
85
  puts e
71
86
  puts e.inspect
72
87
  puts e.backtrace
73
88
  ensure
74
- package.cleanup
89
+ reactor.package.cleanup
75
90
  end
76
91
  end
77
92
 
78
93
  class Convert < Thor
79
94
  extend ThorExt::Start
80
- desc "jekyll SITE_DIRECTORY OUTPUT_DIRECTORY", "Convert a Jekyll site directory to a Capsium package"
81
- def jekyll(site_directory, output_directory)
82
- converter = Capsium::Converters::Jekyll.new(site_directory, output_directory)
95
+
96
+ desc "jekyll PACKAGE_FILE OUTPUT_DIRECTORY",
97
+ "Convert a Capsium package to a Jekyll site"
98
+
99
+ def jekyll(package_file, output_directory)
100
+ converter = Capsium::Converters::Jekyll.new(package_file,
101
+ output_directory)
83
102
  converter.convert
84
103
  end
85
104
  end
@@ -90,7 +109,7 @@ module Capsium
90
109
  desc "reactor SUBCOMMAND ...ARGS", "Manage the reactor"
91
110
  subcommand "reactor", Capsium::Cli::Reactor
92
111
 
93
- desc "converter SUBCOMMAND ...ARGS", "Convert from another format"
94
- subcommand "converter", Capsium::Cli::Convert
112
+ desc "convert SUBCOMMAND ...ARGS", "Convert from another format"
113
+ subcommand "convert", Capsium::Cli::Convert
95
114
  end
96
115
  end
@@ -1,58 +1,91 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # lib/capsium/converters/jekyll_to_capsium.rb
3
+ # lib/capsium/converters/jekyll.rb
4
4
  require "fileutils"
5
5
  require "json"
6
- require "capsium/packager"
6
+ require "capsium/package"
7
7
 
8
8
  module Capsium
9
9
  module Converters
10
10
  class Jekyll
11
- def initialize(site_directory, output_directory)
12
- @site_directory = site_directory
11
+ def initialize(package_file, output_directory)
12
+ @package_file = package_file
13
13
  @output_directory = output_directory
14
14
  end
15
15
 
16
16
  def convert
17
- validate_site_directory
18
- package_directory = prepare_package_directory
19
- packager = Capsium::Packager.new(package_directory)
20
- packager.pack
21
- cleanup_package_directory(package_directory)
22
- end
17
+ package = Capsium::Package.new(@package_file)
23
18
 
24
- private
19
+ prepare_output_directory
25
20
 
26
- def validate_site_directory
27
- return if Dir.exist?(@site_directory) && File.exist?(File.join(@site_directory, "index.html"))
21
+ write_config_yml(package)
22
+ copy_content_files(package)
23
+ generate_index_html(package)
28
24
 
29
- raise "Invalid Jekyll site directory: #{@site_directory}"
25
+ puts "Capsium package converted to Jekyll site at #{@output_directory}"
30
26
  end
31
27
 
32
- def prepare_package_directory
33
- package_directory = File.join(@output_directory, "capsium_package")
34
- FileUtils.mkdir_p(package_directory)
28
+ private
35
29
 
36
- FileUtils.cp_r(Dir.glob("#{@site_directory}/*"), package_directory)
30
+ def prepare_output_directory
31
+ FileUtils.mkdir_p(@output_directory)
32
+ end
37
33
 
38
- create_manifest(package_directory)
34
+ def write_config_yml(package)
35
+ config = {
36
+ "title" => package.metadata.name || "Capsium Jekyll Site",
37
+ "description" => package.metadata.description || "Generated from Capsium package",
38
+ "baseurl" => "",
39
+ "url" => "",
40
+ "markdown" => "kramdown",
41
+ "theme" => "minima",
42
+ }
43
+ write_file("_config.yml", config.to_yaml)
44
+ end
39
45
 
40
- package_directory
46
+ def write_file(relative_path, content)
47
+ output_path = File.join(@output_directory, relative_path)
48
+ FileUtils.mkdir_p(File.dirname(output_path))
49
+ File.write(output_path, content)
41
50
  end
42
51
 
43
- def create_manifest(package_directory)
44
- manifest = {
45
- "name" => "jekyll_site",
46
- "version" => "1.0.0",
47
- "description" => "A Jekyll site converted to a Capsium package",
48
- "files" => Dir.glob("#{package_directory}/**/*").map { |file| file.sub("#{package_directory}/", "") }
49
- }
52
+ def copy_content_files(package)
53
+ package.content_files.each do |file_path|
54
+ relative_path = Pathname.new(file_path).relative_path_from(Pathname.new(package.instance_variable_get(:@path)))
55
+ output_path = File.join(@output_directory, relative_path)
56
+ FileUtils.mkdir_p(File.dirname(output_path))
57
+ FileUtils.cp(file_path, output_path)
58
+ end
59
+ end
50
60
 
51
- File.write(File.join(package_directory, "manifest.json"), JSON.pretty_generate(manifest))
61
+ def generate_index_html(package)
62
+ root_route = package.routes.config.routes.find do |route|
63
+ route.path == "/"
64
+ end
65
+ if root_route
66
+ index_path = File.join(package.instance_variable_get(:@path),
67
+ root_route.target.file)
68
+ index_content = File.read(index_path)
69
+ write_file("index.html", index_content)
70
+ else
71
+ write_file("index.html", default_index_html(package))
72
+ end
52
73
  end
53
74
 
54
- def cleanup_package_directory(package_directory)
55
- FileUtils.rm_rf(package_directory)
75
+ def default_index_html(package)
76
+ <<~HTML
77
+ <!DOCTYPE html>
78
+ <html>
79
+ <head>
80
+ <meta charset="utf-8">
81
+ <title>#{package.metadata.name || 'Capsium Jekyll Site'}</title>
82
+ </head>
83
+ <body>
84
+ <h1>Welcome to #{package.metadata.name || 'Capsium Jekyll Site'}</h1>
85
+ <p>This site was generated from a Capsium package.</p>
86
+ </body>
87
+ </html>
88
+ HTML
56
89
  end
57
90
  end
58
91
  end
@@ -1,78 +1,65 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # lib/capsium/package/dataset.rb
4
3
  require "json"
5
4
  require "yaml"
6
5
  require "csv"
7
6
  require "sqlite3"
7
+ require "shale"
8
+ require "json-schema"
9
+ require_relative "dataset_config"
8
10
 
9
11
  module Capsium
10
12
  class Package
11
- class Dataset < Shale::Mapper
12
- # {
13
- # "datasets": [
14
- # {
15
- # "name": "animals",
16
- # "source": "data/animals.yaml",
17
- # "format": "yaml",
18
- # "schema": "data/animals_schema.yaml"
19
- # }
20
- # ]
21
- # }
22
- attr_reader :name, :path, :type, :data
13
+ class Dataset
14
+ attr_reader :config, :data, :data_path
23
15
 
24
- def initialize(path, data_path)
25
- @path = path
26
- @name = File.basename(@path, ".*")
27
- @type = detect_type
28
- @data_path = data_path
16
+ extend Forwardable
17
+
18
+ def_delegators :@config, :to_json
19
+
20
+ def initialize(config:, data_path: nil)
21
+ @config = config
22
+ @data_path = data_path || config.source
29
23
  @data = load_data
30
24
  end
31
25
 
32
- def detect_type
33
- case File.extname(@path).downcase
34
- when /.ya?ml/ then :yaml
35
- when ".json" then :json
36
- when ".csv" then :csv
37
- when ".tsv" then :tsv
38
- when ".sqlite", ".db" then :sqlite
26
+ def load_data
27
+ case @config.format
28
+ when "yaml" then YAML.load_file(@data_path)
29
+ when "json" then JSON.parse(File.read(@data_path))
30
+ when "csv" then CSV.read(@data_path, headers: true)
31
+ when "tsv" then CSV.read(@data_path, col_sep: "\t", headers: true)
32
+ when "sqlite" then load_sqlite_data
39
33
  else
40
- raise "Unsupported data file type: #{File.extname(@path)}"
34
+ raise "Unsupported data file type: #{@config.format}"
41
35
  end
42
36
  end
43
37
 
44
- def load_data
45
- case @type
46
- when :yaml then YAML.load_file(@path)
47
- when :json then JSON.parse(File.read(@path))
48
- when :csv then CSV.read(@path, headers: true)
49
- when :tsv then CSV.read(@path, col_sep: "\t", headers: true)
50
- when :sqlite then load_sqlite_data
38
+ def validate
39
+ return unless @config.schema
40
+
41
+ schema_path = File.join(File.dirname(@data_path), @config.schema)
42
+ schema = YAML.load_file(schema_path) if @config.format == "yaml"
43
+ schema = JSON.parse(File.read(schema_path)) if @config.format == "json"
44
+
45
+ case @config.format
46
+ when "yaml" then YAML.load_file(@data_path)
47
+ when "json" then JSON.parse(File.read(@data_path))
51
48
  else
52
- raise "Unsupported data file type: #{@type}"
49
+ raise "Validation is only supported for YAML and JSON formats"
53
50
  end
54
- end
55
-
56
- def as_json
57
- {
58
- name: name,
59
- source: relative_path(path),
60
- format: type.to_s
61
- }
62
- end
63
51
 
64
- def to_json(*_args)
65
- JSON.pretty_generate(as_json)
52
+ JSON::Validator.validate!(schema, @data.to_json)
66
53
  end
67
54
 
68
- def relative_path(path)
69
- Pathname.new(path).relative_path_from(@data_path).to_s
55
+ def save_to_file(output_path)
56
+ File.write(output_path, to_json)
70
57
  end
71
58
 
72
59
  private
73
60
 
74
61
  def load_sqlite_data
75
- db = SQLite3::Database.new(@path)
62
+ db = SQLite3::Database.new(@data_path)
76
63
  tables = db.execute("SELECT name FROM sqlite_master WHERE type='table';")
77
64
  data = {}
78
65
  tables.each do |table|
@@ -0,0 +1,28 @@
1
+ require "shale"
2
+
3
+ module Capsium
4
+ class Package
5
+ class DatasetConfig < Shale::Mapper
6
+ attribute :name, Shale::Type::String
7
+ attribute :source, Shale::Type::String
8
+ attribute :format, Shale::Type::String
9
+ attribute :schema, Shale::Type::String
10
+
11
+ def to_dataset(data_path)
12
+ Dataset.new(config: self,
13
+ data_path: File.join(
14
+ File.dirname(data_path), source
15
+ ))
16
+ end
17
+
18
+ def self.from_dataset(dataset)
19
+ new(
20
+ name: dataset.config.name,
21
+ source: dataset.config.source,
22
+ format: dataset.config.format,
23
+ schema: dataset.config.schema,
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
@@ -4,24 +4,25 @@
4
4
  require "json"
5
5
  require "marcel"
6
6
  require "shale"
7
+ require_relative "manifest_config"
7
8
 
8
9
  module Capsium
9
10
  class Package
10
11
  class Manifest
11
- attr_accessor :path, :content_path, :data
12
+ extend Forwardable
13
+ attr_accessor :path, :content_path, :config
14
+
15
+ def_delegators :@config, :to_json
12
16
 
13
17
  def initialize(path)
14
18
  @path = path
15
- # This is {package-name}/content
16
- # or
17
- # /tmp/../content
18
- @content_path = File.join(File.dirname(@path), "content")
19
+ @content_path = File.join(File.dirname(@path), Package::CONTENT_DIR)
19
20
 
20
- @data = if File.exist?(path)
21
- ManifestData.from_json(File.read(path))
22
- else
23
- ManifestData.new(content: generate_manifest)
24
- end
21
+ @config = if File.exist?(path)
22
+ ManifestConfig.from_json(File.read(path))
23
+ else
24
+ ManifestConfig.new(content: generate_manifest)
25
+ end
25
26
  end
26
27
 
27
28
  def generate_manifest
@@ -30,31 +31,28 @@ module Capsium
30
31
  end
31
32
 
32
33
  files.sort.map do |file_path|
33
- ManifestDataItem.new(
34
+ ManifestConfigItem.new(
34
35
  file: relative_path(file_path),
35
- mime: mime_from_path(file_path)
36
+ mime: mime_from_path(file_path),
36
37
  )
37
38
  end
38
39
  end
39
40
 
40
41
  def lookup(filename)
41
- @data.content.detect do |data_item|
42
+ @config.content.detect do |data_item|
42
43
  data_item.file == filename
43
44
  end
44
45
  end
45
46
 
46
- def to_json(*_args)
47
- @data.to_json
48
- end
49
-
50
47
  def save_to_file(output_path = @path)
51
- File.open(output_path, "w") do |file|
52
- file.write(to_json)
53
- end
48
+ @config.content.sort_by!(&:file)
49
+ File.write(output_path, to_json)
54
50
  end
55
51
 
56
52
  def path_to_content_file(path)
57
- Pathname.new(@content_path).join(path)
53
+ raise TypeError, "Path cannot be nil" if path.nil?
54
+
55
+ Pathname.new(File.dirname(@path)).join(path)
58
56
  end
59
57
 
60
58
  def content_file_exists?(path)
@@ -62,7 +60,7 @@ module Capsium
62
60
  end
63
61
 
64
62
  def relative_path(path)
65
- Pathname.new(path).relative_path_from(@content_path).to_s
63
+ Pathname.new(path).relative_path_from(File.dirname(@path)).to_s
66
64
  end
67
65
 
68
66
  private
@@ -71,27 +69,9 @@ module Capsium
71
69
  Marcel::MimeType.for(
72
70
  Pathname.new(path),
73
71
  name: File.basename(path),
74
- extension: File.extname(path)
72
+ extension: File.extname(path),
75
73
  )
76
74
  end
77
75
  end
78
-
79
- class ManifestDataItem < Shale::Mapper
80
- attribute :file, Shale::Type::String
81
- attribute :mime, Shale::Type::String
82
-
83
- # json do
84
- # map "file", to: :file
85
- # map "mime", to: :mime
86
- # end
87
- end
88
-
89
- class ManifestData < Shale::Mapper
90
- attribute :content, ManifestDataItem, collection: true
91
-
92
- # json do
93
- # map "content", to: :content
94
- # end
95
- end
96
76
  end
97
77
  end
@@ -0,0 +1,19 @@
1
+ require "shale"
2
+
3
+ module Capsium
4
+ class Package
5
+ class ManifestConfigItem < Shale::Mapper
6
+ attribute :file, Shale::Type::String
7
+ attribute :mime, Shale::Type::String
8
+ end
9
+
10
+ class ManifestConfig < Shale::Mapper
11
+ attribute :content, ManifestConfigItem, collection: true
12
+
13
+ def sort!
14
+ @content.sort_by!(&:file)
15
+ self
16
+ end
17
+ end
18
+ end
19
+ end