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.4.17 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.17pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.16 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.16pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.15 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.15pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.14 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.14pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.13 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.13pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.12 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.12pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.11 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.10 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.10pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.9.2 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.9.2pre1 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.9.2pre lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.9.1 lib/metanorma/cli/template_repo.rb
metanorma-cli-1.4.9.1pre lib/metanorma/cli/template_repo.rb