Module: Simrb

Defined in:
lib/simrb/comd.rb,
lib/simrb/help.rb,
lib/simrb/info.rb,
lib/simrb/config.rb,
lib/simrb/tool_start.rb

Overview

this file stores the base info of this project of the software

Defined Under Namespace

Modules: Stool Classes: Scommand

Constant Summary collapse

Info =
{
  :name      => 'simrb',
  :created   => '2014-01-01',
  :alias_name    =>  '3s',
  :version   => '1.0.4',
  :author      => 'Linyu Deng',
  :email     =>  '[email protected]',
  :homepage    =>  'https://github.com/simrb/simrb-gem',
  :license   => 'MIT',
  :summary   => 'Simrb',
  :description =>  'This is a command helper for simrb'
}
Spath =

basic path definition

{
  # root path of project
  :module          => 'modules/',
  :public          => 'public/',
  :db_dir          => 'db/',
  :upload_dir        => 'db/upload/',
  :backup_dir        => 'db/backup/',
  :tmp_dir       => 'tmp/',
  :cache_dir       => 'tmp/cache/simrb/',
  :install_lock_file   => 'tmp/install.lock',
  :log_dir       => 'log/',
  :server_log        => 'log/thin.log',
  :command_log     => 'log/command_error_log.html',

  # sub path under module directory of project
  :tool          => '/tool/',
  :logic         => '/logic/',
  :store         => '/boxes/',
  :lang          => '/boxes/langs/',
  :doc         => '/boxes/docs/',
  :schema          => '/boxes/migrations/',
  :install       => '/boxes/installs/',
  :modinfo       => '/boxes/installs/_mods',
  :vars          => '/boxes/installs/_vars',
  :menu          => '/boxes/installs/_menu',
  :tpl         => '/boxes/tpls/',
  :layout_css        => '/boxes/tpls/layout.css',
  :common_css        => '/boxes/tpls/common.css',
  :misc          => '/boxes/misc/',
  :gemfile       => '/boxes/misc/Gemfile',
  :view          => '/views/',
  :assets          => '/views/assets/',
  :gitignore       => '/.gitignore',
  :route         => '/routes.rb',
  :readme          => '/README.md',
}
Scfg =

default settings of scfg file

{
  :time_types        => ['created', 'changed'],
  :fixnum_types      => ['order', 'level'],
  :number_types      => ['Fixnum', 'Integer', 'Float'],
  :field_alias     => {int:'Fixnum', str:'String', text:'Text', time:'Time', big:'Bignum', fl:'Float'},
  :init_module_path    => [:store, :lang, :schema, :install, :modinfo, :misc, :gemfile, :view, :assets, :readme, :route],
  :init_root_path      => [:db_dir, :upload_dir, :backup_dir, :tmp_dir, :log_dir, :module],
  :environment       => 'development',            # or production, test
  :only_enable_modules => [],
  :disable_modules   => [],
  :encoding        => 'utf-8',
  :lang          => 'en',
  :install_lock      => 'yes',
  :db_connection     => 'sqlite://db/data.db',
  :server_log_mode   => 'file',
  :repo_source     => 'https://github.com/',
  :server        => 'thin',
  :bind          => '0.0.0.0',
  :port          => 3000,
  :init_scfg_item      => [:lang, :db_connection, :environment, :bind, :port],
}

Class Method Summary collapse

Class Method Details

.help(args = []) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simrb/help.rb', line 9

def self.help args = []
  res    = []
  i      = 0
  docs_key   = {}
  docs_val   = {}
  Sdocs.each do | key, val |
    docs_key[i] = key
    docs_val[i] = val
    i = i + 1
  end

  if args.empty?
    res << 'please select the number before the list to see detials'
    docs_key.each do | i, key |
      res << "#{i.to_s}, #{key}"
    end
  else
    args.each do | i |
      res << (docs_val.include?(i.to_i) ? docs_val[i.to_i] : 'no document')
    end
  end
  res
end

.input_format(args = []) ⇒ Object

format the input argument from an array to two item, first item is orgin array, last is an hash option

Example

args, opts = Simrb.input_format [“test”, “test2”, “–test”, “–name=test2”, “-n=test3”]

the above is same as

args, opts = Simrb.input_format [“–test”, “test”, “test2”, “–name=test2”, “-n=test3”] the options that starts with “-” you can write any positions of argument

output

args = [“test”, “test2”] opts = true, name: test2, n:test3



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/simrb/config.rb', line 106

def input_format args = []
  resa = [] # return an array
  resh = {} # return an hash
  unless args.empty?
    args.each do | item |

      if item[0] == "-"
        new_item = item.split("-").uniq.last
        if new_item.index "="
          key, val = new_item.split "="
          resh[key.to_sym] = val
        else
          resh[new_item.to_sym] = true
        end
      else
        resa << item
      end

    end
  end
  [resa, resh]
end

.module_loadObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simrb/config.rb', line 43

def module_load
  dirs     = []
  module_ds  = {}

  # get the path of module
  if Scfg[:only_enable_modules].empty?
    dirs = Dir["#{Spath[:module]}*"]
  else
    Scfg[:only_enable_modules].each do | name |
      path = "#{Spath[:module]}#{name}"
      dirs << path if File.exist?(path)
    end
  end

  # load the info of module
  dirs.each do | path |
    path  = "#{path}#{Spath[:modinfo]}"
    content = Simrb.yaml_read path
    name  = content[0]["name"]
    order = (content[0]["order"] || 99)
    module_ds[name] = order unless Scfg[:disable_modules].include?(name.to_s)
  end

  # sort the module by order field
  res    = []
  module_ds  = module_ds.sort_by { |k, v| v }
  module_ds.each do | item |
    res << item[0]
  end
  res
end

.p(args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/simrb/config.rb', line 26

def p args
  res = ""

  if args.class.to_s == 'Array'
    res = args.join("\n")
  elsif args.class.to_s == 'Hash'
    args.each do | k, v |
      res << "#{k.to_s.ljust(15)} => #{v}\n"
    end
    res = res.chomp "\n"
  else
    res = args.to_s
  end

  puts "="*30 + "\n" + res + "\n" + "="*30
end

.path_init(path, content = "") ⇒ Object



75
76
77
78
79
# File 'lib/simrb/config.rb', line 75

def path_init path, content = ""
  unless File.exist?(path)
    path[-1] == '/' ? Dir.mkdir(path) : File.open(path, 'w+') {|f| f.write content}
  end
end

.root_dir_forceObject



81
82
83
84
85
86
# File 'lib/simrb/config.rb', line 81

def root_dir_force
  unless File.exist? 'scfg'
    Simrb.p "Current command only allow to be used under root directory of project"
    exit
  end
end

.yaml_read(path) ⇒ Object



12
13
14
15
16
17
# File 'lib/simrb/config.rb', line 12

def yaml_read path
  require 'yaml'
  YAML.load_file path
rescue
  []
end

.yaml_write(path, data) ⇒ Object



19
20
21
22
23
24
# File 'lib/simrb/config.rb', line 19

def yaml_write path, data
  require "yaml"
  File.open(path, 'w+') do | f |
    f.write data.to_yaml
  end
end