<commit_before>require 'fileutils'

module Amber
  class CLI

    def initialize(root, *args)
      @options = {}
      @options = args.pop if args.last.is_a?(::Hash)
      @root    = File.expand_path(root)
    end

    def init(options)
      new_dir = options[:arg]
      mkdir(new_dir, nil)
      mkdir('amber', new_dir)
      touch('amber/config.rb', new_dir)
      touch('amber/menu.txt', new_dir)
      mkdir('amber/layouts', new_dir)
      mkdir('amber/locales', new_dir)
      mkdir('public', new_dir)
      mkdir('pages', new_dir)
    end

    def build(options)
      site = Site.new(@root)
      site.load_pages
      site.render
    end

    def clear(options)
      site = Site.new(@root)
      site.clear
    end

    def clean(options)
      clear(options)
    end

    def rebuild(options)
      site = Site.new(@root)
      site.continue_on_error = false
      site.load_pages
      FileUtils.mkdir_p(site.dest_dir) unless File.exists?(site.dest_dir)
      gitkeep = File.exists?(File.join(site.dest_dir, '.gitkeep'))
      temp_render = File.join(File.dirname(site.dest_dir), 'public-tmp')
      temp_old_pages = File.join(File.dirname(site.dest_dir), 'remove-me')
      site.with_destination(temp_render) do
        site.render
      end
      FileUtils.mv(site.dest_dir, temp_old_pages)
      FileUtils.mv(temp_render, site.dest_dir)
      site.with_destination(temp_old_pages) do
        site.clear
        FileUtils.rm_r(temp_old_pages)
      end
      if gitkeep
        FileUtils.touch(File.join(site.dest_dir, '.gitkeep'))
      end
    ensure
      # cleanup if something goes wrong.
      FileUtils.rm_r(temp_render) if temp_render && File.exists?(temp_render)
      FileUtils.rm_r(temp_old_pages) if temp_old_pages && File.exists?(temp_old_pages)
    end

    def server(options)
      require 'amber/server'
      host = nil, port = nil
      if options[:arg]
        host, port = options[:arg].split(':')
      end
      if host.nil? || host.empty?<commit_msg>fix bug in `amber server`<commit_after>require 'fileutils'

module Amber
  class CLI

    def initialize(root, *args)
      @options = {}
      @options = args.pop if args.last.is_a?(::Hash)
      @root    = File.expand_path(root)
    end

    def init(options)
      new_dir = options[:arg]
      mkdir(new_dir, nil)
      mkdir('amber', new_dir)
      touch('amber/config.rb', new_dir)
      touch('amber/menu.txt', new_dir)
      mkdir('amber/layouts', new_dir)
      mkdir('amber/locales', new_dir)
      mkdir('public', new_dir)
      mkdir('pages', new_dir)
    end

    def build(options)
      site = Site.new(@root)
      site.load_pages
      site.render
    end

    def clear(options)
      site = Site.new(@root)
      site.clear
    end

    def clean(options)
      clear(options)
    end

    def rebuild(options)
      site = Site.new(@root)
      site.continue_on_error = false
      site.load_pages
      FileUtils.mkdir_p(site.dest_dir) unless File.exists?(site.dest_dir)
      gitkeep = File.exists?(File.join(site.dest_dir, '.gitkeep'))
      temp_render = File.join(File.dirname(site.dest_dir), 'public-tmp')
      temp_old_pages = File.join(File.dirname(site.dest_dir), 'remove-me')
      site.with_destination(temp_render) do
        site.render
      end
      FileUtils.mv(site.dest_dir, temp_old_pages)
      FileUtils.mv(temp_render, site.dest_dir)
      site.with_destination(temp_old_pages) do
        site.clear
        FileUtils.rm_r(temp_old_pages)
      end
      if gitkeep
        FileUtils.touch(File.join(site.dest_dir, '.gitkeep'))
      end
    ensure
      # cleanup if something goes wrong.
      FileUtils.rm_r(temp_render) if temp_render && File.exists?(temp_render)
      FileUtils.rm_r(temp_old_pages) if temp_old_pages && File.exists?(temp_old_pages)
    end

    def server(options)
      require 'amber/server'
      host = nil
      port = nil
      if options[:arg]
        host, port = options[:arg].split(':')
      end
      if host.nil? || host.empty?
{'fraction_non_alphanumeric': 0.11783837878408807, 'fraction_numerical': 0.0, 'mean_word_length': 2.5792300805729633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2046829', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1325, 'n_words': 251}