<commit_before># encoding: UTF-8

require 'thor'

module Spontaneous
  module Cli
    class Base < ::Spontaneous::Cli::Thor
      Spontaneous = ::Spontaneous
      include Thor::Actions
      namespace :default

      class InvalidGenerator < Error
        attr_reader :name
        def initialize(name)
          @name = name
          super()
        end
      end







      desc :console, "Gives you console access to the current site"
      def console
        ENV["SPOT_MODE"] = "console"
        prepare :console
        ARGV.clear
        require 'irb'
        boot!
        IRB.setup(nil)
        irb = IRB::Irb.new
        IRB.conf[:MAIN_CONTEXT] = irb.context
        irb.context.evaluate("require 'irb/completion'", 0)
        irb.context.evaluate("require '#{File.expand_path(File.dirname(__FILE__) + '/console')}'", 0)
        # irb.context.evaluate("include Spontaneous", 0)
        trap("SIGINT") do
          irb.signal_handle
        end
        catch(:IRB_EXIT) do
          irb.eval_input
        end
      end

      desc :generate, "Executes the Spontaneous generator with given options."
      def generate(*args)
        require File.expand_path('../../../spontaneous', __FILE__)
        ARGV.shift
        generator_name = ARGV.shift
        generator = nil
        d = Spontaneous::Generators
        case generator_name
        when ''
          raise InvalidGenerator.new(generator_name)
        when 'site'
          generator = d::Site
        when 'page'
          prepare(:generator)
          boot!
          generator = d::Page
        when /[a-zA-Z0-9-]+(\.[a-z]+)+/
          # generator called as 'spot generate domain.com'
          ARGV.unshift(generator_name)
          generator = d::Site
        else
          raise InvalidGenerator.new(generator_name)
        end
        generator.start(ARGV) if generator
      rescue InvalidGenerator => e
        say "Unrecognised generator '#{e.name}'. Available options are:\n\n  #{available_generators.join("\n  ")}\n"
      end

      desc :browse, "Launces a browser pointing to the current development CMS"
      def browse
        prepare :browse
        require 'launchy'
        boot!
        ::Launchy::Browser.run("http://localhost:#{Site.config.port}/@spontaneous")
      end


      desc :init, "Creates databases and initialises a new Spontaneous site"
      def init
        prepare :init
        site = Spontaneous::Site.instantiate(Dir.pwd, options.environment, :back)
        require File.expand_path('../../../spontaneous', __FILE__)
        Sequel.extension :migration
        connection_params = Spontaneous.db_settings
        connection_params[:user] = 'root'
        database = connection_params.delete(:database)
        password = connection_params.delete(:password)
        catch(:error) do
          Sequel.connect(connection_params) do |connection|
            ["", "_test"].map { |ext| "#{database}#{ext}"}.each do |db|
              begin
                say "  >> Creating database `#{db}`"
                connection.run("CREATE DATABASE `#{db}` CHARACTER SET UTF8")
              rescue => e
                say " >>> Unable to create #{connection_params[:adapter]} database `#{db}`:\n   > #{e}", :red
                # throw :error
              end
            end
          end
          boot!
          Spontaneous.database.logger = nil
          say "  >> Running migrations..."
          Sequel::Migrator.apply(Spontaneous.database, Spontaneous.gem_dir('db/migrations'))
          say "  >> Done"
        end
      end

      desc :migrate, "Runs migrations"
      def migrate
        prepare :init
        boot!
        Sequel.extension :migration
        connection_params = Spontaneous.db_settings
        say "  >> Running migrations..."
        Sequel::Migrator.apply(Spontaneous.database, Spontaneous.gem_dir('db/migrations'))
        say "  >> Done"
      end

      desc :adduser, "Add a user"
      def adduser
        prepare :adduser
        boot!
        users = Spontaneous::Permissions::User.count
        attrs = {}
        width = 12
        valid_login = /^[a-z0-9_]{3,}$/
        levels = Spontaneous::Permissions::UserLevel.all.map(&:to_s)
        level = nil

        say("\nAll fields are required:\n", :green)
        begin
          attrs[:login] = ask "Login : ".rjust(width, " ")
          say("Login must consist of at least 3 letters, numbers and underscores", :red) unless valid_login === attrs[:login]
        end while attrs[:login] !~ valid_login<commit_msg>Run migrations on each of the new databases at init<commit_after># encoding: UTF-8

require 'thor'

module Spontaneous
  module Cli
    class Base < ::Spontaneous::Cli::Thor
      Spontaneous = ::Spontaneous
      include Thor::Actions
      namespace :default

      class InvalidGenerator < Error
        attr_reader :name
        def initialize(name)
          @name = name
          super()
        end
      end







      desc :console, "Gives you console access to the current site"
      def console
        ENV["SPOT_MODE"] = "console"
        prepare :console
        ARGV.clear
        require 'irb'
        boot!
        IRB.setup(nil)
        irb = IRB::Irb.new
        IRB.conf[:MAIN_CONTEXT] = irb.context
        irb.context.evaluate("require 'irb/completion'", 0)
        irb.context.evaluate("require '#{File.expand_path(File.dirname(__FILE__) + '/console')}'", 0)
        # irb.context.evaluate("include Spontaneous", 0)
        trap("SIGINT") do
          irb.signal_handle
        end
        catch(:IRB_EXIT) do
          irb.eval_input
        end
      end

      desc :generate, "Executes the Spontaneous generator with given options."
      def generate(*args)
        require File.expand_path('../../../spontaneous', __FILE__)
        ARGV.shift
        generator_name = ARGV.shift
        generator = nil
        d = Spontaneous::Generators
        case generator_name
        when ''
          raise InvalidGenerator.new(generator_name)
        when 'site'
          generator = d::Site
        when 'page'
          prepare(:generator)
          boot!
          generator = d::Page
        when /[a-zA-Z0-9-]+(\.[a-z]+)+/
          # generator called as 'spot generate domain.com'
          ARGV.unshift(generator_name)
          generator = d::Site
        else
          raise InvalidGenerator.new(generator_name)
        end
        generator.start(ARGV) if generator
      rescue InvalidGenerator => e
        say "Unrecognised generator '#{e.name}'. Available options are:\n\n  #{available_generators.join("\n  ")}\n"
      end

      desc :browse, "Launces a browser pointing to the current development CMS"
      def browse
        prepare :browse
        require 'launchy'
        boot!
        ::Launchy::Browser.run("http://localhost:#{Site.config.port}/@spontaneous")
      end


      desc :init, "Creates databases and initialises a new Spontaneous site"
      def init
        prepare :init
        site = Spontaneous::Site.instantiate(Dir.pwd, options.environment, :back)
        require File.expand_path('../../../spontaneous', __FILE__)
        Sequel.extension :migration
        connection_params = Spontaneous.db_settings
        connection_params[:user] = 'root'
        database = connection_params.delete(:database)
        password = connection_params.delete(:password)
        catch(:error) do
          Sequel.connect(connection_params) do |connection|
            ["", "_test"].map { |ext| "#{database}#{ext}"}.each do |db|
              begin
                say "  >> Creating database `#{db}`"
                connection.run("CREATE DATABASE `#{db}` CHARACTER SET UTF8")
              rescue => e
                say " >>> Unable to create #{connection_params[:adapter]} database `#{db}`:\n   > #{e}", :red
                # throw :error
              end
              begin
                connection.run("USE `#{db}`")
                connection.logger = nil
                say "  >> Running migrations..."
                Sequel::Migrator.apply(connection, Spontaneous.gem_dir('db/migrations'))
                say "  >> Done"
              rescue => e
                say " >>> Error running migrations on database `#{db}`:\n   > #{e}", :red
              end
            end
          end
          boot!
        end
      end

      desc :migrate, "Runs migrations"
      def migrate
        prepare :init
        boot!
        Sequel.extension :migration
        connection_params = Spontaneous.db_settings
        say "  >> Running migrations..."
        Sequel::Migrator.apply(Spontaneous.database, Spontaneous.gem_dir('db/migrations'))
        say "  >> Done"
      end

      desc :adduser, "Add a user"
      def adduser
        prepare :adduser
        boot!
        users = Spontaneous::Permissions::User.count
        attrs = {}
        width = 12
        valid_login = /^[a-z0-9_]{3,}$/
        levels = Spontaneous::Permissions::UserLevel.all.map(&:to_s)
        level = nil

        say("\nAll fields are required:\n", :green)
        begin
          attrs[:login] = ask "Login : ".rjust(width, " ")
          say("Login must consist of at least 3 letters, numbers and underscores", :red) unless valid_login === attrs[:login]
        end while attrs[:login] !~ valid_login
{'fraction_non_alphanumeric': 0.11374919058925102, 'fraction_numerical': 0.002805957263112454, 'mean_word_length': 1.950334288443171, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 40, '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': '3072828', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2590, 'n_words': 647}