DEV: move `discourse_dev` gem to the core. (#13360)

DEV: move discourse_dev gem to the core. (#13360)

And get avatar images from discourse_dev_assets gem.

diff --git a/Gemfile b/Gemfile
index 703c5da..c2680dc 100644
--- a/Gemfile
+++ b/Gemfile
@@ -174,7 +174,8 @@ group :development do
   gem 'binding_of_caller'
   gem 'yaml-lint'
   gem 'annotate'
-  gem 'discourse_dev'
+  gem 'discourse_dev_assets'
+  gem 'faker', "~> 2.16"
 end
 
 # this is an optional gem, it provides a high performance replacement
diff --git a/Gemfile.lock b/Gemfile.lock
index 428edc7..95ae17f 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -115,7 +115,7 @@ GEM
       railties (>= 3.1)
     discourse-ember-source (3.12.2.3)
     discourse-fonts (0.0.8)
-    discourse_dev (0.2.1)
+    discourse_dev_assets (0.0.2)
       faker (~> 2.16)
     docile (1.4.0)
     ecma-re-validator (0.3.0)
@@ -504,12 +504,13 @@ DEPENDENCIES
   discourse-ember-rails (= 0.18.6)
   discourse-ember-source (~> 3.12.2)
   discourse-fonts
-  discourse_dev
+  discourse_dev_assets
   email_reply_trimmer
   ember-handlebars-template (= 0.8.0)
   excon
   execjs
   fabrication
+  faker (~> 2.16)
   fakeweb
   fast_blank
   fast_xs
diff --git a/config/dev_defaults.yml b/config/dev_defaults.yml
new file mode 100644
index 0000000..4c5fbd6
--- /dev/null
+++ b/config/dev_defaults.yml
@@ -0,0 +1,38 @@
+site_settings:
+  tagging_enabled: true
+  verbose_discourse_connect_logging: true
+
+seed: 1
+start_date: 2020-01-01
+auth_plugin_enabled: true
+allow_anonymous_to_impersonate: false
+
+category:
+  count: 30
+group:
+  count: 15
+post:
+  include_images: false
+  max_likes_count: 10
+tag:
+  count: 30
+topic:
+  count: 30
+  replies:
+    # number of replies per topic between min and max
+    min: 0
+    max: 12
+    overrides:
+      # topic titles can be found in config/locales/faker.en.yml
+      - title: "Coolest thing you have seen today"
+        count: 99
+  tags:
+    # number of tags per topic between min and max
+    min: 0
+    max: 3
+user:
+  count: 30
+
+new_user:
+   username: new_user
+   email: new_user@example.com
diff --git a/config/routes.rb b/config/routes.rb
index e395cd0..6946889 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -977,10 +977,6 @@ Discourse::Application.routes.draw do
     post "/do-not-disturb" => "do_not_disturb#create"
     delete "/do-not-disturb" => "do_not_disturb#destroy"
 
-    if Rails.env.development?
-      mount DiscourseDev::Engine => "/dev/"
-    end
-
     get "*url", to: 'permalinks#show', constraints: PermalinkConstraint.new
   end
 end
diff --git a/lib/discourse_dev.rb b/lib/discourse_dev.rb
new file mode 100644
index 0000000..45774b0
--- /dev/null
+++ b/lib/discourse_dev.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module DiscourseDev
+  def self.config
+    @config ||= Config.new
+  end
+
+  def self.settings_file
+    File.join(root, "config", "settings.yml")
+  end
+
+  def self.root
+    File.expand_path("..", __dir__)
+  end
+end
diff --git a/lib/discourse_dev/category.rb b/lib/discourse_dev/category.rb
new file mode 100644
index 0000000..b13c43d
--- /dev/null
+++ b/lib/discourse_dev/category.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+require 'discourse_dev/record'
+require 'rails'
+require 'faker'
+
+module DiscourseDev
+  class Category < Record
+
+    def initialize
+      super(::Category, DiscourseDev.config.category[:count])
+      @parent_category_ids = ::Category.where(parent_category_id: nil).pluck(:id)
+    end
+
+    def data
+      name = Faker::Discourse.unique.category
+      parent_category_id = nil
+
+      if Faker::Boolean.boolean(true_ratio: 0.6)
+        offset = Faker::Number.between(from: 0, to: @parent_category_ids.count - 1)
+        parent_category_id = @parent_category_ids[offset]
+        @permissions = ::Category.find(parent_category_id).permissions_params.presence
+      else
+        @permissions = nil
+      end
+
+      {
+        name: name,
+        description: Faker::Lorem.paragraph,
+        user_id: ::Discourse::SYSTEM_USER_ID,
+        color: Faker::Color.hex_color.last(6),
+        parent_category_id: parent_category_id
+      }
+    end
+
+    def permissions
+      return @permissions if @permissions.present?
+      return { everyone: :full } if Faker::Boolean.boolean(true_ratio: 0.75)
+
+      permission = {}
+      group = Group.random
+      permission[group.id] = Faker::Number.between(from: 1, to: 3)
+
+      permission
+    end
+
+    def create!
+      super do |category|
+        category.set_permissions(permissions)
+        category.save!
+
+        @parent_category_ids << category.id if category.parent_category_id.blank?
+      end
+    end
+
+    def self.random
+      super(::Category)
+    end
+  end
+end
diff --git a/lib/discourse_dev/config.rb b/lib/discourse_dev/config.rb
new file mode 100644
index 0000000..4a2142b
--- /dev/null
+++ b/lib/discourse_dev/config.rb
@@ -0,0 +1,149 @@
+# frozen_string_literal: true
+
+require 'rails'
+require 'highline/import'
+
+module DiscourseDev
+  class Config
+    attr_reader :config, :file_path
+
+    def initialize
+      default_file_path = File.join(Rails.root, "config", "dev_defaults.yml")
+      @file_path = File.join(Rails.root, "config", "dev.yml")
+      default_config = YAML.load_file(default_file_path)
+
+      if File.exists?(file_path)
+        user_config = YAML.load_file(file_path)
+      else
+        puts "I did no detect a custom `config/dev.yml` file, creating one for you where you can amend defaults."
+        FileUtils.cp(default_file_path, file_path)
+        user_config = {}
+      end
+
+      @config = default_config.deep_merge(user_config).deep_symbolize_keys
+    end
+
+    def update!
+      update_site_settings
+      create_admin_user
+      create_new_user
+      set_seed
+    end
+
+    private
+
+    def update_site_settings
+      puts "Updating site settings..."
+
+      site_settings = config[:site_settings] || {}
+
+      site_settings.each do |key, value|
+        puts "#{key} = #{value}"
+        SiteSetting.set(key, value)
+      end
+
+      SiteSetting.refresh!
+    end
+
+    def create_admin_user
+      puts "Creating default admin user account..."
+
+      settings = config[:admin]
+
+      if settings.present?
+        create_admin_user_from_settings(settings)
+      else
+        create_admin_user_from_input
+      end
+    end
+
+    def create_new_user
+      settings = config[:new_user]
+
+      if settings.present?
+        email = settings[:email] || "new_user@example.com"
+
+        new_user = ::User.create!(
+          email: email,
+          username: settings[:username] || UserNameSuggester.suggest(email)
+        )
+        new_user.email_tokens.update_all confirmed: true
+        new_user.activate
+      end
+    end
+
+    def set_seed
+      seed = self.seed || 1
+      Faker::Config.random = Random.new(seed)
+    end
+
+    def start_date
+      DateTime.parse(config[:start_date])
+    end
+
+    def method_missing(name)
+      config[name.to_sym]
+    end
+
+    def create_admin_user_from_settings(settings)
+      email = settings[:email]
+
+      admin = ::User.with_email(email).first_or_create!(
+        email: email,
+        username: settings[:username] || UserNameSuggester.suggest(email),
+        password: settings[:password]
+      )
+      admin.grant_admin!
+      if admin.trust_level < 1
+        admin.change_trust_level!(1)
+      end
+      admin.email_tokens.update_all confirmed: true
+      admin.activate
+    end
+
+    def create_admin_user_from_input
+      begin
+        email = ask("Email:  ")
+        password = ask("Password (optional, press ENTER to skip):  ")
+        username = UserNameSuggester.suggest(email)
+
+        admin = ::User.new(
+          email: email,
+          username: username
+        )
+
+        if password.present?
+          admin.password = password
+        else
+          puts "Once site is running use https://localhost:9292/user/#{username}/become to access the account in development"
+        end
+
+        admin.name = ask("Full name:  ") if SiteSetting.full_name_required

[... diff too long, it was truncated ...]

GitHub sha: 6abc45e57b1b011b8050baa8f109990ec796ff3e

This commit appears in #13360 which was approved by davidtaylorhq. It was merged by vinothkannans.