Class: TDiary::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/tdiary/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



9
10
11
# File 'lib/tdiary/cli.rb', line 9

def self.source_root
	File.expand_path('../../..', __FILE__)
end

Instance Method Details

#assets_copyObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tdiary/cli.rb', line 59

def assets_copy
	require 'tdiary'
	assets_path = File.join(TDiary.server_root, 'public/assets')
	TDiary::Application.new.assets_paths.each do |path|
		Dir.glob(File.join(path, '*')).each do |entity|
			if File.directory?(entity)
				directory entity, File.join(assets_path, File.basename(entity))
			else
				copy_file entity, File.join(assets_path, File.basename(entity))
			end
		end
	end
end

#htpasswdObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tdiary/cli.rb', line 134

def htpasswd
	require 'webrick/httpauth/htpasswd'
	say "Input your username/password"
	print 'Username: '
	ARGV.replace([])
	username = gets().chop
	print 'New password: '
	system "stty -echo"
	password = $stdin.gets.chop
	puts
	print 'Re-type new password: '
	password2 = $stdin.gets.chop
	puts
	system "stty echo"
	if password != password2
		raise StandardError, 'password verification error'
	end
	htpasswd = WEBrick::HTTPAuth::Htpasswd.new(ENV['HTPASSWD'] || '.htpasswd')
	htpasswd.set_passwd(nil, username, password)
	htpasswd.flush
end

#new(name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tdiary/cli.rb', line 16

def new(name)
	target = File.join(Dir.pwd, name)
	deploy(target)
	copy_file('tdiary.conf.beginner', File.join(target, 'tdiary.conf'))
	template('misc/templates/Gemfile.local.erb', File.join(target, 'Gemfile.local'))

	unless options[:'skip-bundle']
		Bundler.with_clean_env do
			inside(target) do
				run('bundle install --without test development')
				run('bundle exec tdiary htpasswd')
			end
		end
	else
		say "run `bundle install && bundle exec tdiary htpasswd` manually", :red
	end
	say 'install finished', :green
	say "run `tdiary server` in #{name} directory to start server", :green
end

#serverObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/tdiary/cli.rb', line 100

def server
	require 'tdiary'
	require 'tdiary/environment'

	if options[:cgi]
		opts = {
			daemon: ENV['DAEMON'],
			bind: options[:bind],
			port: options[:port],
			logger: $stderr,
			access_log: options[:log] ? File.open(options[:log], 'a') : $stderr
		}
		TDiary::Server.run( opts )
	elsif
		# --rack option
		# Rack::Server reads ARGV as :config, so delete it
		require 'webrick'
		ARGV.shift
		opts = {
			environment: ENV['RACK_ENV'] || "development",
			daemonize: false,
			Host: options[:bind],
			Port: options[:port],
			pid: File.expand_path("tdiary.pid"),
			config: File.expand_path("config.ru")
		}
		if options[:log]
			opts[:AccessLog] = [[File.open(options[:log], 'a'), WEBrick::AccessLog::CLF]]
		end
		::Rack::Server.start( opts )
	end
end

#testObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tdiary/cli.rb', line 74

def test
	target = File.join(Dir.pwd, 'tmp/test')
	deploy(target)
	append_to_file(File.join(target, 'Gemfile'), "path '#{CLI::source_root}'")
	directory('spec', File.join(target, 'spec'))
	directory('test', File.join(target, 'test'))

	Bundler.with_clean_env do
		inside(target) do
			run('bundle install')
			run('bundle exec rake spec')
		end
	end
end

#updateObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tdiary/cli.rb', line 39

def update
	target = Dir.pwd
	unless in_tdiary_dir?(target)
		say "please run update command in your tdiary directory", :red
		return 1
	end

	deploy(target)

	unless options[:'skip-bundle']
		Bundler.with_clean_env do
			inside(target) do
				run('bundle install --without test development')
			end
		end
	end
	say 'update finished', :green
end

#versionObject



157
158
159
# File 'lib/tdiary/cli.rb', line 157

def version
	say "tdiary #{TDiary::VERSION}"
end