Sha256: 11852658b699317f0299f7e6951e0928e6f85a4999435c7d0b134cc5db0797f0

Contents?: true

Size: 1.69 KB

Versions: 98

Compression:

Stored size: 1.69 KB

Contents

require "yaml"

require "metanorma/cli/stringify_all_keys"

module Metanorma
  module Cli
    class TemplateRepo
      def initialize(options = {})
        @name = options.fetch(:name)
        @source = options.fetch(:source)
        @type = options.fetch(:type, "custom")
        @overwrite = options.fetch(:overwrite, false)
      end

      def add
        create_template_config
        add_new_template(name, source, type)
        write_to_template_config(templates)

        templates[:templates]
      end

      def self.add(name, source, options = {})
        new(options.merge(name: name, source: source)).add
      end

      private

      attr_reader :name, :source, :type, :overwrite

      def templates
        @templates ||= YAML.load_file(template_config_file).symbolize_all_keys
      end

      def template_config_file
        @template_config_file ||= Cli.config_path(true)
      end

      def create_template_config
        unless template_config_file.exist?
          unless template_config_file.dirname.exist?
            FileUtils.mkdir_p(template_config_file.dirname)
          end

          write_to_template_config(templates: [])
        end
      end

      def write_to_template_config(templates)
        shash = templates.stringify_all_keys
        File.write(template_config_file, shash.to_yaml)
      end

      def add_new_template(name, source, type)
        names = templates[:templates].map { |template| template[:name].to_s }

        if names.include?(name.to_s) && overwrite == false
          raise Errors::DuplicateTemplateError.new("Duplicate metanorma template")
        end

        templates[:templates].push({ name: name, source: source, type: type })
      end
    end
  end
end

Version data entries

98 entries across 98 versions & 1 rubygems

Version Path
metanorma-cli-1.5.24 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.23 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.23.pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.22 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.22.pre.1 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.22.pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.21 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.21.pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.20 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.20.pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.19 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.19.pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.18 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.18.pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.17 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.17.pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.16 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.16.pre.pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.15 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.5.15.pre.alpha.2 lib/metanorma/cli/template_repo.rb