fanforce-app-factory 2.0.0.rc5 → 2.0.0.rc7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Rakefile +54 -3
  4. data/apps.json +8 -8
  5. data/lib/fanforce/app_factory.rb +19 -16
  6. data/lib/fanforce/app_factory/Rakefile +6 -14
  7. data/lib/fanforce/app_factory/Routes.rb +2 -2
  8. data/lib/fanforce/app_factory/app.rb +19 -8
  9. data/lib/fanforce/app_factory/cli.rb +5 -0
  10. data/lib/fanforce/app_factory/cli/bitbucket.rb +70 -0
  11. data/lib/fanforce/app_factory/cli/bundler.rb +21 -0
  12. data/lib/fanforce/app_factory/cli/env.rb +99 -0
  13. data/lib/fanforce/app_factory/cli/git.rb +58 -0
  14. data/lib/fanforce/app_factory/cli/iron.rb +61 -0
  15. data/lib/fanforce/app_factory/cli/pow.rb +46 -0
  16. data/lib/fanforce/app_factory/cli/scaffolding.rb +74 -0
  17. data/lib/fanforce/app_factory/cli/scaffolding_file.rb +510 -0
  18. data/lib/fanforce/app_factory/cli/scripts/config.rb +9 -0
  19. data/lib/fanforce/app_factory/cli/scripts/create.rb +64 -0
  20. data/lib/fanforce/app_factory/cli/scripts/destroy.rb +61 -0
  21. data/lib/fanforce/app_factory/cli/scripts/push.rb +70 -0
  22. data/lib/fanforce/app_factory/cli/scripts/restart.rb +40 -0
  23. data/lib/fanforce/app_factory/cli/scripts/setup.rb +70 -0
  24. data/lib/fanforce/app_factory/cli/scripts/status.rb +120 -0
  25. data/lib/fanforce/app_factory/cli/scripts/update.rb +41 -0
  26. data/lib/fanforce/app_factory/cli/uranium.rb +77 -0
  27. data/lib/fanforce/app_factory/cli/utils.rb +39 -0
  28. data/lib/fanforce/app_factory/cli_directory_of_apps.rb +50 -0
  29. data/lib/fanforce/app_factory/cli_single_app.rb +49 -0
  30. data/lib/fanforce/app_factory/controllers/base_controller.rb +0 -4
  31. data/lib/fanforce/app_factory/core_config.rb +25 -26
  32. data/lib/fanforce/app_factory/load.rb +8 -0
  33. data/lib/fanforce/app_factory/load_iron_worker.rb +2 -0
  34. data/lib/fanforce/app_factory/load_sprockets.rb +2 -0
  35. data/lib/fanforce/app_factory/scaffolding/._.gitignore.registry +1 -0
  36. data/lib/fanforce/app_factory/scaffolding/._Gemfile.registry +2 -0
  37. data/lib/fanforce/app_factory/scaffolding/._Rakefile.registry +2 -0
  38. data/lib/fanforce/app_factory/scaffolding/._config.json.registry +2 -0
  39. data/lib/fanforce/app_factory/scaffolding/._config.ru.registry +2 -0
  40. data/lib/fanforce/app_factory/scaffolding/._favicon.ico.registry +1 -0
  41. data/lib/fanforce/app_factory/scaffolding/._robots.txt.registry +2 -0
  42. data/lib/fanforce/app_factory/scaffolding/.gitignore +27 -0
  43. data/lib/fanforce/app_factory/scaffolding/Gemfile +9 -0
  44. data/lib/fanforce/app_factory/scaffolding/Rakefile +7 -0
  45. data/lib/fanforce/app_factory/{scafolding → scaffolding}/Routes.rb +5 -1
  46. data/lib/fanforce/app_factory/scaffolding/assets/css/._engagement.scss.registry +2 -0
  47. data/lib/fanforce/app_factory/scaffolding/assets/css/._fembedded.scss.registry +2 -0
  48. data/lib/fanforce/app_factory/scaffolding/assets/css/._promotional.scss.registry +3 -0
  49. data/lib/fanforce/app_factory/{scafolding/scafolding_assets → scaffolding/assets}/css/engagement.scss +8 -5
  50. data/lib/fanforce/app_factory/scaffolding/assets/css/fembedded.scss +22 -0
  51. data/lib/fanforce/app_factory/{scafolding/scafolding_assets → scaffolding/assets}/css/promotional.scss +9 -4
  52. data/lib/fanforce/app_factory/scaffolding/assets/img/._icon-16.png.registry +1 -0
  53. data/lib/fanforce/app_factory/scaffolding/assets/img/._icon-32.png.registry +1 -0
  54. data/lib/fanforce/app_factory/scaffolding/assets/img/._icon-42.png.registry +1 -0
  55. data/lib/fanforce/app_factory/scaffolding/assets/img/._marketplace.png.registry +1 -0
  56. data/lib/fanforce/app_factory/{scafolding/scafolding_assets → scaffolding/assets}/img/icon-16.png +0 -0
  57. data/lib/fanforce/app_factory/{scafolding/scafolding_assets → scaffolding/assets}/img/icon-32.png +0 -0
  58. data/lib/fanforce/app_factory/{scafolding/scafolding_assets → scaffolding/assets}/img/icon-42.png +0 -0
  59. data/lib/fanforce/app_factory/{scafolding/scafolding_assets → scaffolding/assets}/img/marketplace.png +0 -0
  60. data/lib/fanforce/app_factory/scaffolding/assets/img/promotional/._fanforce_bg.png.registry +1 -0
  61. data/lib/fanforce/app_factory/scaffolding/assets/img/promotional/._icon-42.png.registry +1 -0
  62. data/lib/fanforce/app_factory/scaffolding/assets/img/promotional/._icon-plus.png.registry +1 -0
  63. data/lib/fanforce/app_factory/{scafolding/scafolding_assets → scaffolding/assets}/img/promotional/fanforce_bg.png +0 -0
  64. data/lib/fanforce/app_factory/{scafolding/scafolding_assets → scaffolding/assets}/img/promotional/icon-42.png +0 -0
  65. data/lib/fanforce/app_factory/{scafolding/scafolding_assets → scaffolding/assets}/img/promotional/icon-plus.png +0 -0
  66. data/lib/fanforce/app_factory/scaffolding/assets/js/._fembedded-app.coffee.registry +2 -0
  67. data/lib/fanforce/app_factory/scaffolding/assets/js/._fembedded.js.registry +2 -0
  68. data/lib/fanforce/app_factory/scaffolding/assets/js/._promotional.js.registry +2 -0
  69. data/lib/fanforce/app_factory/scaffolding/assets/js/fembedded-app.coffee +11 -0
  70. data/lib/fanforce/app_factory/scaffolding/assets/js/fembedded.js +12 -0
  71. data/lib/fanforce/app_factory/scaffolding/assets/js/lib/._cookie.coffee.registry +2 -0
  72. data/lib/fanforce/app_factory/scaffolding/assets/js/lib/._filters.coffee.registry +2 -0
  73. data/lib/fanforce/app_factory/scaffolding/assets/js/lib/._utils.coffee.registry +2 -0
  74. data/lib/fanforce/app_factory/scaffolding/assets/js/lib/cookie.coffee +24 -0
  75. data/lib/fanforce/app_factory/scaffolding/assets/js/lib/filters.coffee +9 -0
  76. data/lib/fanforce/app_factory/scaffolding/assets/js/lib/utils.coffee +40 -0
  77. data/lib/fanforce/app_factory/scaffolding/assets/js/promotional.js +4 -0
  78. data/lib/fanforce/app_factory/scaffolding/config.json +24 -0
  79. data/lib/fanforce/app_factory/scaffolding/config.ru +8 -0
  80. data/lib/fanforce/app_factory/{scafolding/public → scaffolding}/favicon.ico +0 -0
  81. data/lib/fanforce/app_factory/scaffolding/layouts/._engagement.haml.registry +1 -0
  82. data/lib/fanforce/app_factory/scaffolding/layouts/._fembedded.haml.registry +2 -0
  83. data/lib/fanforce/app_factory/scaffolding/layouts/._promotional.haml.registry +1 -0
  84. data/lib/fanforce/app_factory/{scafolding → scaffolding}/layouts/engagement.haml +5 -4
  85. data/lib/fanforce/app_factory/scaffolding/layouts/fembedded.haml +26 -0
  86. data/lib/fanforce/app_factory/scaffolding/layouts/promotional.haml +19 -0
  87. data/lib/fanforce/app_factory/scaffolding/robots.txt +7 -0
  88. data/lib/fanforce/app_factory/scaffolding/views/._index.haml.registry +1 -0
  89. data/lib/fanforce/app_factory/{scafolding → scaffolding}/views/index.haml +13 -7
  90. data/lib/fanforce/app_factory/sinatra/_load.rb +7 -7
  91. data/lib/fanforce/app_factory/sinatra/helper_routes/app.rb +2 -2
  92. data/lib/fanforce/app_factory/sinatra/helper_routes/com_behavior.rb +2 -2
  93. data/lib/fanforce/app_factory/sinatra/helper_routes/com_broadcaster.rb +1 -1
  94. data/lib/fanforce/app_factory/sinatra/helper_routes/com_connector.rb +2 -2
  95. data/lib/fanforce/app_factory/sprockets/compiler.rb +0 -1
  96. data/lib/fanforce/app_factory/version.rb +1 -1
  97. data/workers.json +8 -8
  98. metadata +80 -38
  99. data/lib/fanforce/app_factory/asset_framework/app_factory/directives/internal-iframe/internal-iframe.scss +0 -1
  100. data/lib/fanforce/app_factory/asset_framework/app_factory/lib/cookie.coffee +0 -16
  101. data/lib/fanforce/app_factory/asset_framework/app_factory/lib/url.coffee +0 -20
  102. data/lib/fanforce/app_factory/asset_framework/app_factory/lib/utils.coffee +0 -11
  103. data/lib/fanforce/app_factory/asset_framework/app_factory/scafolding/_engagement.scss +0 -1
  104. data/lib/fanforce/app_factory/asset_framework/app_factory/scafolding/_promotional.scss +0 -1
  105. data/lib/fanforce/app_factory/asset_framework/app_factory/scafolding/_standard.scss +0 -1
  106. data/lib/fanforce/app_factory/asset_framework/app_factory/scafolding/promotional.coffee +0 -0
  107. data/lib/fanforce/app_factory/cli/_cleanorgs.rb +0 -54
  108. data/lib/fanforce/app_factory/scafolding/public/robots.txt +0 -2
  109. data/lib/fanforce/app_factory/scafolding/scafolding_assets/css/standard.scss +0 -10
  110. data/lib/fanforce/app_factory/scafolding/scafolding_assets/img/marketplace.psd +0 -0
  111. data/lib/fanforce/app_factory/scafolding/views/add_initiative.haml +0 -1
  112. data/lib/fanforce/app_factory/scafolding/views/add_source.haml +0 -1
  113. data/lib/fanforce/app_factory/scafolding/views/close_popup.haml +0 -8
  114. data/lib/fanforce/app_factory/scafolding/views/config.haml +0 -1
  115. data/lib/fanforce/app_factory/scafolding/views/convert_initiative.haml +0 -1
  116. data/lib/fanforce/app_factory/scafolding/views/dashboard.haml +0 -38
  117. data/lib/fanforce/app_factory/scafolding/views/edit_initiative.haml +0 -1
  118. data/lib/fanforce/app_factory/scafolding/views/engage.haml +0 -1
  119. data/lib/fanforce/app_factory/scafolding/views/new_message.haml +0 -1
  120. data/lib/fanforce/app_factory/scafolding/views/source_details.haml +0 -1
  121. data/lib/fanforce/app_factory/scafolding/views/widget_templates.haml +0 -1
  122. data/lib/fanforce/app_factory/sprockets/_all.rb +0 -2
@@ -0,0 +1,58 @@
1
+ class Fanforce::AppFactory::CLI::Git
2
+ include Fanforce::AppFactory::CLI::Utils
3
+
4
+ attr_reader :app
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def setup
11
+ if init.include?('Reinitialized')
12
+ puts "#{'Found '.format(:green,:bold)}" + 'existing local repository'
13
+ else
14
+ puts "#{'Initialized '.format(:green,:bold)}" + 'local repository'
15
+ add_files and log '- git add .'
16
+ make_first_commit and log '- git commit -m "initial fanforce commit"'
17
+ puts "#{'Created '.format(:green,:bold)}" + 'initial fanforce commit'
18
+ end
19
+ end
20
+
21
+ def self.status_table_header(app_column_width)
22
+ sprintf('%-12s %-50s %85s', 'GIT STATUS', 'APP NAME', 'STATS OVERVIEW').format(:bold)
23
+ end
24
+
25
+ def status_row(app_column_width)
26
+ sprintf("%s%-12s#{fmt_end} %-50s %28s %28s %28s",
27
+ fmt_start(is_committed ? :green : :red),
28
+ (is_committed ? 'Committed' : 'Uncommitted'),
29
+ app.name,
30
+ *changed_stats
31
+ )
32
+ end
33
+
34
+ def is_committed
35
+ `git status`.include?('nothing to commit') ? true : false
36
+ end
37
+
38
+ def changed_stats
39
+ `git diff --stat`.split("\n").last.split(', ')
40
+ end
41
+
42
+ private
43
+
44
+ def init
45
+ response = `git init`
46
+ `git config core.fileMode false`
47
+ return response
48
+ end
49
+
50
+ def add_files
51
+ `git add .`
52
+ end
53
+
54
+ def make_first_commit
55
+ `git commit -m "initial fanforce commit"`
56
+ end
57
+
58
+ end
@@ -0,0 +1,61 @@
1
+ class Fanforce::AppFactory::CLI::Iron
2
+ include Fanforce::AppFactory::CLI::Utils
3
+
4
+ require_gem 'iron_worker_ng', 'iron_worker_ng'
5
+
6
+ attr_reader :app
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def upload(environment)
13
+ if !File.directory?("#{app.dir}/workers")
14
+ return log "#{'Skipped '.format(:bold)} no workers folder was found"
15
+ end
16
+
17
+ env = Fanforce::AppFactory::CLI::Env.new(app)
18
+ vars = env.app_vars(environment) || {}
19
+
20
+ return puts "#{'Skipped '.format(:bold)} #{environment.to_s.titleize} is missing IRON_TOKEN and/or IRON_PROJECT_ID env variables" if vars['IRON_TOKEN'].blank? or vars['IRON_PROJECT_ID'].blank?
21
+
22
+ puts "#{'Updating Env'.format(:green,:bold)} #{environment.to_s.titleize}... and workers have #{vars.size} env variables"
23
+ env.push_env_to(environment, vars, true)
24
+
25
+ iron_worker = IronWorkerNG::Client.new(:token => vars['IRON_TOKEN'], :project_id => vars['IRON_PROJECT_ID'])
26
+ Dir.chdir("#{app.dir}/workers") do
27
+ workers = Dir['*.worker']
28
+ return puts "#{'Skipped '.format(:bold)} #{environment.to_s.titleize} has 0 workers" if workers.size == 0
29
+
30
+ upload_processes = []
31
+ FileUtils.cp("#{app.dir}/workers/.env/#{environment}.rb", "#{app.dir}/workers/.env/.#{environment}env.rb")
32
+ workers.each do |filename|
33
+ code_name = filename.gsub('.worker', '')
34
+
35
+ puts "#{'Uploading'.format(:green,:bold)} #{filename.gsub('.worker', '')} to #{environment.to_s.titleize}..."
36
+ code = IronWorkerNG::Code::Base.new(:workerfile => "#{app.dir}/workers/#{filename}")
37
+ code.remote
38
+ code.name = code_name
39
+ code.file("#{app.dir}/workers/.env/.#{environment}env.rb")
40
+
41
+ upload_processes << upload_iron_worker(iron_worker, code, filename, environment)
42
+ end
43
+ upload_processes.each { |pid| Process.waitpid(pid) }
44
+ FileUtils.remove_file("#{app.dir}/workers/.env/.#{environment}env.rb")
45
+ end
46
+ end
47
+
48
+ def upload_iron_worker(iron_worker, code, filename, environment)
49
+ fork do
50
+ begin
51
+ iron_worker.codes.create(code, max_concurrency: 5)
52
+ rescue Exception => e
53
+ puts "#{'Aborted '.format(:red,:bold)} upload of #{filename.gsub('.worker', '')} to #{environment.to_s.titleize}..."
54
+ puts e.message
55
+ puts e.backtrace
56
+ puts ''
57
+ end
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1,46 @@
1
+ class Fanforce::AppFactory::CLI::Pow
2
+ include Fanforce::AppFactory::CLI::Utils
3
+
4
+ attr_reader :app
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def setup
11
+ create_domain(app.root_domain)
12
+ create_domain(Fanforce.default_short_domain)
13
+ end
14
+
15
+ def destroy
16
+ destroy_domain(app.root_domain)
17
+ destroy_domain(Fanforce.default_short_domain)
18
+ end
19
+
20
+ def remove_domain_extension(domain)
21
+ domain.gsub(/\.[a-z]+$/, '')
22
+ end
23
+
24
+ def create_domain(root_domain)
25
+ domain = "#{app._id}.#{root_domain.gsub(/\.([a-z]+)$/, '')}"
26
+ domain_extension = $1
27
+ symlink = "#{ENV['HOME']}/.pow/#{domain}"
28
+
29
+ File.delete(symlink) if File.exists?(symlink) or File.symlink?(symlink)
30
+ `ln -s #{app.dir} #{symlink}`
31
+ puts "#{'Connected'.format(:bold,:green)} #{domain}.#{domain_extension} to #{app.dir}/"
32
+ end
33
+
34
+ def destroy_domain(root_domain)
35
+ domain = "#{app._id}.#{root_domain.gsub(/\.([a-z]+)$/, '')}"
36
+ domain_extension = $1
37
+ symlink = "#{ENV['HOME']}/.pow/#{domain}"
38
+ if File.exists?(symlink)
39
+ File.delete(symlink)
40
+ puts "#{'Removed'.format(:bold,:green)} #{domain}.#{domain_extension}"
41
+ else
42
+ puts "#{'Already Removed'.format(:bold,:green)} #{domain}.#{domain_extension} to #{app.dir}/"
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,74 @@
1
+ class Fanforce::AppFactory::CLI::Scaffolding
2
+ include Fanforce::AppFactory::CLI::Utils
3
+
4
+ require_relative 'scaffolding_file'
5
+
6
+ attr_reader :app
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ error 'Your .fanforce-app-factory file must specify a default fanforce_version_dependency' if !config[:fanforce_version_dependency]
11
+ end
12
+
13
+ def setup
14
+ if File.directory?("#{app.dir}/tmp")
15
+ log "#{'Found '.format(:green,:bold)} #{app.dir}/tmp/"
16
+ else
17
+ Dir.mkdir("#{app.dir}/tmp")
18
+ log "#{'Created'.format(:green,:bold)} #{app.dir}/tmp/"
19
+ end
20
+ files.each {|file| file.create }
21
+ end
22
+
23
+ def update
24
+ files_updated = false
25
+ files.each {|file| files_updated = true if file.update }
26
+ return files_updated
27
+ end
28
+
29
+ def metadata
30
+ # status = :exists, :current, :missing, :previous, :diverged, :forked
31
+ change_data = {}
32
+ files.each do |file|
33
+ next change_data[file.filepath] = {
34
+ status: File.directory?(file.filepath) ? :exists : :missing,
35
+ } if File.directory?(file.scaffold_filepath)
36
+
37
+ status = file.status
38
+ diff = file.diff_changes if [:previous,:diverged].include?(status)
39
+ insertions = 0
40
+ deletions = 0
41
+ diff.lines do |line|
42
+ next deletions += 1 if line[0] == '-'
43
+ next insertions += 1 if line[0] == '+'
44
+ end if diff
45
+
46
+ change_data[file.filepath] = {
47
+ status: status,
48
+ diff: diff,
49
+ insertions: insertions,
50
+ deletions: deletions
51
+ }
52
+ end
53
+ return change_data
54
+ end
55
+
56
+ ######################################################################################################################
57
+
58
+ def files
59
+ Dir.glob("#{self.class.dir}/**/*", File::FNM_DOTMATCH).inject([]) do |files, scaffold_filepath|
60
+ next files if scaffold_filepath =~ /\.registry$/
61
+ next files if scaffold_filepath =~ /\/\.\.?$/
62
+ scaffold_filepath = "#{scaffold_filepath}/" if File.directory?(scaffold_filepath) and scaffold_filepath !~ /\/$/
63
+ files << Fanforce::AppFactory::CLI::ScaffoldingFile.new(scaffold_filepath, app)
64
+ end
65
+ end
66
+
67
+ ######################################################################################################################
68
+
69
+ def self.dir
70
+ File.expand_path('scaffolding', Fanforce::AppFactory.base_dir)
71
+ end
72
+
73
+ end
74
+
@@ -0,0 +1,510 @@
1
+ class Fanforce::AppFactory::CLI::ScaffoldingFile
2
+ include Fanforce::AppFactory::CLI::Utils
3
+
4
+ require 'digest'
5
+ require 'multi_json'
6
+ require_gem 'diffy', 'diffy'
7
+
8
+ attr_reader :scaffold_filepath, :app
9
+
10
+ def initialize(scaffold_filepath, app=nil)
11
+ @scaffold_filepath = scaffold_filepath
12
+ @app = app
13
+ if app
14
+ @filepath = "#{app.dir}/" + scaffold_filepath.gsub("#{Fanforce::AppFactory::CLI::Scaffolding.dir}/", '')
15
+ @relativepath = extract_relativepath(@filepath)
16
+ end
17
+ end
18
+
19
+ def create
20
+ if File.directory?(scaffold_filepath)
21
+ mkdir
22
+ return nil
23
+ end
24
+
25
+ return update if File.exists?(filepath)
26
+
27
+ contents = File.open(scaffold_filepath).read
28
+ File.open(filepath, 'w') {|f| f.write(cleanse_content(contents)) }
29
+ log "#{'Created'.format(:green,:bold)} #{relativepath}"
30
+ return true
31
+ end
32
+
33
+ def update
34
+ if File.directory?(scaffold_filepath)
35
+ mkdir
36
+ return nil
37
+ end
38
+
39
+ is_missing = is_missing?
40
+ if !is_missing
41
+ return if is_corrupted?
42
+ return if is_forked?
43
+ return if is_current_version?
44
+ end
45
+
46
+ content = updated_content
47
+ File.open(filepath, 'w') {|f| f.write(content) }
48
+
49
+ log "#{(is_missing ? 'Created' : 'Updated').format(:green,:bold)} #{relativepath}"
50
+ return true
51
+ end
52
+
53
+ def status
54
+ return @status unless @status.nil?
55
+ if is_missing?
56
+ @status = :missing
57
+ elsif is_corrupted?
58
+ @status = :corrupted
59
+ elsif is_forked?
60
+ @status = :forked
61
+ elsif is_previous_version?
62
+ @status = :previous
63
+ elsif is_diverged?
64
+ @status = :diverged
65
+ elsif is_current_version?
66
+ @status = :current
67
+ else
68
+ error "Unknown status for #{relativepath}"
69
+ end
70
+ end
71
+
72
+ def diff_changes
73
+ return nil if scaffold_filepath =~ /\/Routes\.rb$/
74
+ return nil if is_missing?
75
+ return nil if is_image_file?
76
+
77
+ original_contents = File.open(filepath).read
78
+ Diffy::Diff.new(original_contents, updated_content).to_s(:text)
79
+ end
80
+
81
+ ######################################################################################################################
82
+
83
+ def mkdir
84
+ Dir.mkdir(filepath) if !File.directory?(filepath)
85
+ end
86
+
87
+ def extension
88
+ @extension ||= scaffold_filepath.match(/([^\/\.]+)$/)[1].to_s.downcase
89
+ end
90
+
91
+ def extension_type
92
+ @extension_type ||= extension.downcase.to_sym
93
+ end
94
+
95
+ ######################################################################################################################
96
+
97
+ def scaffold_type(filepath=scaffold_filepath)
98
+ # :file, :image, :partial, :items, :json, :skipped
99
+ @scaffold_types ||= {}
100
+ return @scaffold_types[filepath] unless @scaffold_types[filepath].nil?
101
+ return if !File.exists?(filepath)
102
+
103
+ if is_image_file?
104
+ type = :image
105
+ elsif filepath =~ /\/.+\.json$/
106
+ type = :json
107
+ else
108
+ first_line = File.open(filepath).read.split("\n").first
109
+ if first_line
110
+ matches = first_line.match(/\s*\S+\s*AppFactoryScaffolding\s*:\s*(FILE|PARTIAL|ITEMS|SKIPPED)/)
111
+ type = matches[1].downcase.to_sym if matches and matches[1]
112
+ end
113
+ end
114
+ raise "scaffold_type could not be read from file: #{filepath}" if !type
115
+ raise "scaffold_type (#{type}) is unknown: #{lines.first}" if ![:file, :image, :partial, :items, :json, :skipped].include?(type)
116
+ @scaffold_types[filepath] = type
117
+ end
118
+
119
+ ######################################################################################################################
120
+
121
+ def is_missing?
122
+ return @is_missing unless @is_missing.nil?
123
+ @is_missing = !File.exists?(filepath)
124
+ end
125
+
126
+ def is_corrupted?
127
+ return @is_corrupted unless @is_corrupted.nil?
128
+ scaffold_type = scaffold_type(filepath)
129
+ if !scaffold_type
130
+ error "The first line of #{relativepath} should specify an AppFactoryScaffolding type"
131
+ elsif scaffold_type == :json
132
+ begin MultiJson.load(File.open(filepath).read)
133
+ rescue Exception => e; error "Corrupted #{relativepath}: #{e.message}"; end
134
+ elsif scaffold_type == :partial
135
+ File.open(filepath).read.match(/^\s*\S+\s*END PARTIAL/)
136
+ end
137
+ @is_corrupted = false
138
+ end
139
+
140
+ def is_previous_version?
141
+ return @is_previous_version unless @is_previous_version.nil?
142
+ return @is_previous_version = false if is_current_version?
143
+
144
+ # :file, :image, :partial, :items, :json, :skipped
145
+ if [:file,:image,:partial].include?(scaffold_type)
146
+ @is_previous_version = digest_exists_in_registry?(current_digest)
147
+ elsif [:items].include?(scaffold_type)
148
+ list_exists_in_registry?(File.open(filepath).read)
149
+ elsif [:json].include?(scaffold_type)
150
+ json_exists_in_registry?(File.open(filepath).read)
151
+ elsif [:skipped].include?(scaffold_type)
152
+ return @is_previous_version = false
153
+ else
154
+ raise 'unknown scaffold_type'
155
+ end
156
+ end
157
+
158
+ def is_forkable?
159
+ return @is_forkable unless @is_forkable.nil?
160
+ @is_forkable = !(scaffold_filepath =~ /\/(Routes\.rb|config\.json|.gitignore)$/)
161
+ end
162
+
163
+ def is_image_file?
164
+ return @is_image_file unless @is_image_file.nil?
165
+ @is_image_file = scaffold_filepath =~ /\.(png|jpg|gif|ico)$/
166
+ end
167
+
168
+ def is_forked?
169
+ return @is_forked unless @is_forked.nil?
170
+ return @is_forked = true if is_image_file? and !is_current_version? and !is_previous_version?
171
+ return @is_forked = false if !is_forkable?
172
+ @is_forked = File.open(filepath, &:readline).include?('FORKED')
173
+ end
174
+
175
+ def is_diverged?
176
+ return @is_diverged unless @is_diverged.nil?
177
+ @is_diverged = !is_current_version?
178
+ end
179
+
180
+ def is_current_version?
181
+ return @is_current unless @is_current.nil?
182
+
183
+ # :file, :image, :partial, :items, :json, :skipped
184
+ if [:file,:image].include?(scaffold_type)
185
+ @is_current = (current_digest == scaffold_digest)
186
+ elsif [:partial].include?(scaffold_type)
187
+ @is_current = (current_digest == updated_digest)
188
+ elsif [:items].include?(scaffold_type)
189
+ @is_current = list_matches_latest_registry?(File.open(filepath).read)
190
+ elsif [:json].include?(scaffold_type)
191
+ @is_current = json_matches_latest_registry?(File.open(filepath).read)
192
+ elsif [:skipped].include?(scaffold_type)
193
+ @is_current = true
194
+ else
195
+ raise 'unknown scaffold_type'
196
+ end
197
+ end
198
+
199
+ ######################################################################################################################
200
+
201
+ def registry_filepath
202
+ scaffold_fileparts = scaffold_filepath.split('/')
203
+ "#{scaffold_fileparts[0...-1].join('/')}/._#{scaffold_fileparts[-1]}.registry"
204
+ end
205
+
206
+ def digest_exists_in_registry?(digest)
207
+ return false if !File.exists?(registry_filepath)
208
+
209
+ File.open(registry_filepath, 'r').each do |line|
210
+ version, previous_digest = line.strip.split(/:\s?/)
211
+ return true if digest == previous_digest
212
+ end
213
+ return false
214
+ end
215
+
216
+ def list_registry_rules(&block)
217
+ return @list_registry_rules unless @list_registry_rules.nil? or block
218
+ rules = {ADD: [], REM: [], OPT: []}
219
+ File.open(registry_filepath).each_line do |line|
220
+ rule_changes = MultiJson.load(line.split(':', 2)[1], symbolize_keys: true)
221
+
222
+ rule_changes[:ADD].each do |item|
223
+ item = item.strip
224
+ rules[:ADD] << item unless rules[:ADD].include?(item)
225
+ rules[:REM].delete(item)
226
+ rules[:OPT].delete(item)
227
+ end if rule_changes[:ADD]
228
+
229
+ rule_changes[:REM].each do |item|
230
+ item = item.strip
231
+ rules[:ADD].delete(item)
232
+ rules[:REM] << item unless rules[:REM].include?(item)
233
+ rules[:OPT].delete(item)
234
+ end if rule_changes[:REM]
235
+
236
+ rule_changes[:OPT].each do |item|
237
+ item = item.strip
238
+ rules[:ADD].delete(item)
239
+ rules[:REM].delete(item)
240
+ rules[:OPT] << item unless rules[:OPT].include?(item)
241
+ end if rule_changes[:OPT]
242
+
243
+ block.call(rules) if block
244
+ end
245
+ @list_registry_rules = rules
246
+ end
247
+
248
+ def list_matches_latest_registry?(contents)
249
+ raise if scaffold_type != :items
250
+ list = []
251
+ contents = contents.is_a?(Array) ? contents : contents.split("\n")
252
+ contents.each do |line|
253
+ line = line.strip
254
+ list << line if line.present? and line !~ /^\s*#/
255
+ end
256
+ list_registry_rules[:ADD].each do |item|
257
+ return false if !list.include?(item)
258
+ end
259
+ list_registry_rules[:REM].each do |item|
260
+ return false if list.include?(item)
261
+ end
262
+ return true
263
+ end
264
+
265
+ def list_exists_in_registry?(contents)
266
+ raise if scaffold_type != :items
267
+ list = []
268
+ contents = contents.is_a?(Array) ? contents : contents.split("\n")
269
+ contents.each do |line|
270
+ line = line.strip
271
+ list << line if line.present? and line !~ /^\s*#/
272
+ end
273
+ list_registry_rules do |rules|
274
+ rules[:ADD].each do |item|
275
+ next if !list.include?(item)
276
+ end
277
+ rules[:REM].each do |item|
278
+ next if list.include?(item)
279
+ end
280
+ return true
281
+ end
282
+ return false
283
+ end
284
+
285
+ def json_registry_rules(&block)
286
+ return @json_registry_rules unless @json_registry_rules.nil? or block
287
+ rules = {ADD: {}, REM: {}, OPT: {}}
288
+ File.open(registry_filepath).each_line do |line|
289
+ rule_changes = MultiJson.load(line.split(':', 2)[1], symbolize_keys: true)
290
+ update_json_registry_rules(:ADD, rule_changes[:ADD], rules, '.') if rule_changes[:ADD]
291
+ update_json_registry_rules(:REM, rule_changes[:REM], rules, '.') if rule_changes[:REM]
292
+ update_json_registry_rules(:OPT, rule_changes[:OPT], rules, '.') if rule_changes[:OPT]
293
+ block.call(rules) if block
294
+ end
295
+ @json_registry_rules = rules
296
+ end
297
+
298
+ def update_json_registry_rules(type, rule_changes, rules, key_path)
299
+ rule_changes.each do |rule_key, rule_value|
300
+ next update_json_registry_rules(type, rule_value, rules, "#{key_path}.#{rule_key}") if rule_value.is_a?(Hash)
301
+ if type == :ADD
302
+ json_rule(rules, :ADD, key_path, true)[rule_key] = rule_value
303
+ (rule = json_rule rules, :REM, key_path) && rule.delete(rule_key)
304
+ (rule = json_rule rules, :OPT, key_path) && rule.delete(rule_key)
305
+ elsif type == :REM
306
+ (rule = json_rule rules, :ADD, key_path) && rule.delete(rule_key)
307
+ json_rule(rules, :REM, key_path, true)[rule_key] = rule_value
308
+ (rule = json_rule rules, :OPT, key_path) && rule.delete(rule_key)
309
+ elsif type == :OPT
310
+ (rule = json_rule rules, :ADD, key_path) && rule.delete(rule_key)
311
+ (rule = json_rule rules, :REM, key_path) && rule.delete(rule_key)
312
+ json_rule(rules, :OPT, key_path, true)[rule_key] = rule_value
313
+ end
314
+ end
315
+ end
316
+
317
+ def json_rule(rules, type, key_path, create_hash_if_nil=false)
318
+ rule = rules[type]
319
+ key_path.gsub(/\.\./, '.').split('.').each do |key|
320
+ next if key.blank?
321
+ rule[key.to_sym] ||= {} if create_hash_if_nil
322
+ rule = rule[key.to_sym]
323
+ end
324
+ rule
325
+ end
326
+
327
+ def json_matches_latest_registry?(json)
328
+ hash = MultiJson.load(json, symbolize_keys: true)
329
+ subtract_hash_from_hash!(json_registry_rules[:OPT], hash)
330
+ return false if !hash_keys_match_hash_keys(json_registry_rules[:ADD], hash)
331
+ return false if hash_keys_overlap_hash_keys(json_registry_rules[:REM], hash)
332
+ return true
333
+ end
334
+
335
+ def json_exists_in_registry?(json)
336
+ original_hash = MultiJson.load(json, symbolize_keys: true)
337
+ json_registry_rules do |rules|
338
+ hash = original_hash.clone
339
+ subtract_hash_from_hash!(rules[:OPT], hash)
340
+ next if !hash_keys_match_hash_keys(rules[:ADD], hash)
341
+ next if hash_keys_overlap_hash_keys(rules[:REM], hash)
342
+ return true
343
+ end
344
+ return false
345
+ end
346
+
347
+ def subtract_hash_from_hash!(hash_to_subtract, hash_to_retain)
348
+ hash_to_subtract.each do |k,v|
349
+ next subtract_hash_from_hash!(v, hash_to_retain[k]) if v.is_a?(Hash) and hash_to_retain[k].is_a?(Hash)
350
+ hash_to_retain.delete(k) if hash_to_retain.is_a?(Hash)
351
+ end
352
+ end
353
+
354
+ def hash_keys_match_hash_keys(hash1, hash2)
355
+ hash1.each do |k,v|
356
+ return false if !hash_keys_match_hash_keys(v, hash2[k]) if v.is_a?(Hash) and hash2[k].is_a?(Hash)
357
+ return false if v.is_a?(Hash) and !hash2[k].is_a?(Hash)
358
+ return false if !hash2.has_key?(k)
359
+ end
360
+ return true
361
+ end
362
+
363
+ def hash_keys_overlap_hash_keys(hash1, hash2)
364
+ hash1.each do |k,v|
365
+ return true if hash_keys_overlap_hash_keys(v, hash2[k]) if v.is_a?(Hash) and hash2[k].is_a?(Hash)
366
+ return true if hash2.has_key?(k)
367
+ end
368
+ return false
369
+ end
370
+
371
+ def current_digest
372
+ return @current_digest unless @current_digest.nil?
373
+ if [:image,:file,:partial].include?(scaffold_type)
374
+ @current_digest = Digest::SHA1.file(filepath).hexdigest.strip
375
+ else
376
+ raise "There is no digest scaffold type: #{scaffold_type}"
377
+ end
378
+ end
379
+
380
+ def scaffold_digest
381
+ return @scaffold_digest unless @scaffold_digest.nil?
382
+ if [:image,:file].include?(scaffold_type)
383
+ @scaffold_digest = Digest::SHA1.file(scaffold_filepath).hexdigest.strip
384
+ elsif [:partial].include?(scaffold_type)
385
+ @scaffold_digest = Digest::SHA1.hexdigest(extract_partial(File.open(scaffold_filepath).read, scaffold_filepath)).strip
386
+ elsif [:items].include?(scaffold_type)
387
+ @scaffold_digest = Digest::SHA1.hexdigest(extract_items_from_scaffold).strip
388
+ else
389
+ raise "There is no digest scaffold type: #{scaffold_type}"
390
+ end
391
+ end
392
+
393
+ def updated_digest
394
+ return @updated_digest unless @updated_digest.nil?
395
+ @updated_digest = Digest::SHA1.hexdigest(updated_content).strip
396
+ end
397
+
398
+ def app_factory_line_for_gemfile
399
+ line = "gem 'fanforce-app-factory'"
400
+ return line if !config[:app_factory_gem].is_a?(Hash)
401
+
402
+ line += ", '#{config[:app_factory_gem][:version]}'" if config[:app_factory_gem][:version].present?
403
+ line += ", :path => '#{config[:app_factory_gem][:path]}'" if config[:app_factory_gem][:path].present?
404
+ line += ", :git => '#{config[:app_factory_gem][:git]}'" if config[:app_factory_gem][:git].present?
405
+ line
406
+ end
407
+
408
+ ######################################################################################################################
409
+
410
+ def extract_partial(content, filepath)
411
+ raise if scaffold_type != :partial
412
+ partial = ''
413
+ found_end = false
414
+ content.lines.each do |line|
415
+ break found_end = true if is_line_ending_partial?(line)
416
+ partial += line
417
+ end
418
+ raise "END PARTIAL could not be found for #{extract_relativepath(filepath)}" if !found_end
419
+ return partial
420
+ end
421
+
422
+ def extract_post_partial(content, filepath)
423
+ raise if scaffold_type != :partial
424
+ post_partial = ''
425
+ found_end = false
426
+ content.lines.each do |line|
427
+ found_end ||= is_line_ending_partial?(line) || next
428
+ post_partial += line
429
+ end
430
+ error "END PARTIAL could not be found for #{extract_relativepath(filepath)}" if !found_end
431
+ return post_partial
432
+ end
433
+
434
+ def is_line_ending_partial?(line)
435
+ return true if extension_type == :haml and line =~ /^\s*-#\s*END PARTIAL/
436
+ return true if [:css,:scss,:js].include?(extension_type) and line =~ /^\s*\/\/\s*END PARTIAL/
437
+ return true if [:gemfile,:rb,:gitignore,:ru,:rakefile,:coffee,:txt].include?(extension_type) and line =~ /^\s*#\s*END PARTIAL/
438
+ end
439
+
440
+ def updated_content
441
+ if is_missing? or [:file,:image].include?(scaffold_type)
442
+ File.open(scaffold_filepath).read
443
+ elsif [:partial].include?(scaffold_type)
444
+ scaffold_partial = extract_partial(File.open(scaffold_filepath).read, scaffold_filepath)
445
+ current_post_partial = extract_post_partial(File.open(filepath).read, filepath)
446
+ cleanse_content(scaffold_partial + current_post_partial)
447
+ elsif [:items].include?(scaffold_type)
448
+ content = ''
449
+ items_to_add = list_registry_rules[:ADD].clone
450
+ items_to_rem = list_registry_rules[:REM].clone
451
+ File.open(filepath).read.lines do |line|
452
+ item = line.strip
453
+ content += line if !items_to_rem.include?(item)
454
+ items_to_add.delete(item)
455
+ end
456
+ content += "\n" if content !~ /\n$/
457
+ items_to_add.each do |item|
458
+ content += "#{item}\n"
459
+ end
460
+ cleanse_content(content)
461
+ elsif [:json].include?(scaffold_type)
462
+ hash = MultiJson.load(File.open(filepath).read, symbolize_keys: true)
463
+ json_registry_rules[:ADD].each {|k,v| hash[k] = v if !hash.has_key?(k) }
464
+ json_registry_rules[:REM].each {|k,v| hash.delete(k) }
465
+ cleanse_content(hash)
466
+ end
467
+ end
468
+
469
+ ######################################################################################################################
470
+
471
+ def cleanse_content(contents)
472
+ if scaffold_filepath =~ /\/config\.json$/
473
+ cleanup_config_json(contents)
474
+ elsif scaffold_filepath =~ /\/Gemfile$/
475
+ cleanup_gemfile(contents)
476
+ else
477
+ contents
478
+ end
479
+ end
480
+
481
+ def cleanup_config_json(contents)
482
+ hash = contents.is_a?(Hash) ? contents : MultiJson.load(contents, symbolize_keys: true)
483
+ hash[:_id] ||= app._id
484
+ hash[:name] ||= app._id.titleize
485
+ hash[:description] ||= 'This is where you put a short app description.'
486
+ hash[:fanforce_version_dependency] = config[:fanforce_version_dependency]
487
+ hash[:created_with_app_factory_version] = Fanforce::AppFactory::VERSION
488
+ cleaned = JSON.pretty_generate(hash)
489
+ return cleaned
490
+ end
491
+
492
+ def cleanup_gemfile(contents)
493
+ contents.gsub('gem FANFORCE-APP-FACTORY', app_factory_line_for_gemfile)
494
+ end
495
+
496
+ ######################################################################################################################
497
+
498
+ def filepath
499
+ @filepath || (raise 'app is not set' if !app)
500
+ end
501
+
502
+ def relativepath
503
+ @relativepath || (raise 'app is not set' if !app)
504
+ end
505
+
506
+ def extract_relativepath(filepath)
507
+ filepath.gsub(Fanforce::CLI::DIR, '')
508
+ end
509
+
510
+ end