Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #286 from paxtonhare/dev
Browse files Browse the repository at this point in the history
#150 - fixing jar generation and the way that paths are resolved for fil...
  • Loading branch information
paxtonhare committed Oct 22, 2014
2 parents dea3205 + ec17b18 commit eca8ff3
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 105 deletions.
146 changes: 58 additions & 88 deletions deploy/lib/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
require 'util'

module Roxy
class Framework

Expand All @@ -22,119 +24,108 @@ def initialize(options)
@logger = options[:logger]
@src_dir = options[:properties]["ml.xquery.dir"]
@test_dir = options[:properties]["ml.xquery-test.dir"]

@is_jar = is_jar?

if (@is_jar)
@template_base = 'roxy/lib/templates'
else
@template_base = File.expand_path('../templates', __FILE__)
end
end

def create
if ARGV[0] == "model"
burn = ARGV.shift #burn model arg
create_what = ARGV.shift

case create_what
when "model"
str = ARGV.shift
if str
model, function = str.split('/')
filename = ARGV.shift || model
create_model(model, filename, function) if model != nil
create_object('model', model, function, ARGV.shift || model)
else
ARGV.unshift(burn)
Help.doHelp(logger, "create")
end
elsif ARGV[0] == "test"
burn = ARGV.shift #burn test arg

str = ARGV.shift
if str
when "test"
if str = ARGV.shift
suite, test = str.split('/')
create_suite(suite, test) if suite != nil
else
ARGV.unshift(burn)
Help.doHelp(logger, "create")
end
elsif ARGV[0] == "layout"
burn = ARGV.shift #burn test arg

when "layout"
layout = ARGV.shift
format = ARGV.shift || "html"

if layout != nil
if !layout.nil?
create_layout(layout, format)
else
ARGV.unshift(burn)
Help.doHelp(logger, "create")
end
when nil
Help.doHelp(logger, "create")
else
force = find_arg(['-f']) != nil

str = ARGV.shift
if str
controller, view = str.split('/')

view = view || "main"

format = ARGV.shift || "html"
logger.debug "controller: #{controller}"
logger.debug "view: #{view}\n"
logger.debug "format: #{format}\n"

if controller != nil && view != nil
if force == false
view_type = (format != nil && format != "none") ? ((format == "json") ? "a" : "an") + " #{format} view" : "no view"
print "\nAbout to create a #{view}() function in the #{controller}.xqy controller with #{view_type}. Proceed?\n> "
answer = gets()
answer = answer.downcase.strip
return if answer != "y" && answer != "yes"
end

create_controller(controller, view)
create_view(controller, view, format) unless format == nil || format == "none"
controller, view = create_what.split('/')
view = view || "main"
format = ARGV.shift || "html"
logger.debug "controller: #{controller}\nview: #{view}\n\nformat: #{format}\n"

if !(controller.nil? || view.nil?)
if force == false
view_type = (format != nil && format != "none") ? ((format == "json") ? "a" : "an") + " #{format} view" : "no view"
print "\nAbout to create a #{view}() function in the #{controller}.xqy controller with #{view_type}. Proceed?\n> "
answer = gets().downcase.strip
return if answer != "y" && answer != "yes"
end
else
Help.doHelp(logger, "create")

create_object('controller', controller, view, controller)
create_view(controller, view, format) unless format == nil || format == "none"
end
end
end

def create_model(model, filename, function)
target_file = File.expand_path("#{@src_dir}/app/models/#{filename}.xqy", __FILE__)
model_file = nil
if File.exists? target_file
if function != nil
model_file = File.read target_file
def create_object(type, namespace, function, target_filename)
target_file_path = File.expand_path("#{@src_dir}/app/#{type}s/#{target_filename}.xqy", __FILE__)

if model_file.index("m:#{function}()") != nil
logger.warn "Function #{model}:#{function}() already exists. Skipping..."
if File.exists?(target_file_path)
if (function != nil)
target_file = File.read(target_file_path)

if (target_file.index("#{type[0]}:#{function}(") != nil)
logger.warn "Function #{namespace}:#{function}() already exists. Skipping..."
return
end
else
logger.warn "Model #{model} already exists. Skipping..."
logger.warn "#{type} #{namespace} already exists. Skipping..."
return
end
else
model_file = File.read(File.expand_path('../templates/model.xqy', __FILE__))
model_file.gsub!("#model-name", model)
target_file = read_file(@template_base + "/#{type}.xqy")
target_file.gsub!("##{type}-name", namespace)
end

if function != nil
model_file << File.read(File.expand_path('../templates/model-function.xqy', __FILE__))
model_file.gsub!("#function-name", function)
if !function.nil?
target_file << read_file(@template_base + "/#{type}-function.xqy")
target_file.gsub!("#function-name", function)
end
File.open(target_file, 'w') { |f| f.write(model_file) }

File.open(target_file_path, 'w') { |f| f.write(target_file) }
end

def create_suite(suite, test)
suite_dir = File.expand_path("#{@test_dir}/test/suites/#{suite}/", __FILE__)
suite_dir = File.expand_path("#{@test_dir}/suites/#{suite}/", __FILE__)
Dir.mkdir(suite_dir) unless File.directory? suite_dir

if test
target_file = "#{suite_dir}/#{test}.xqy"

test_file = nil
if File.exists? target_file
logger.warn "Test #{test} already exists. Skipping..."
return
else
test_file = File.read(File.expand_path('../templates/test.xqy', __FILE__))
end

File.open(target_file, 'a') {|f| f.write(test_file) }
File.open(target_file, 'a') {|f| f.write(read_file(@template_base + '/test.xqy')) }
end
end

Expand All @@ -148,33 +139,12 @@ def create_layout(layout, format)
if File.exists? target_file
logger.warn "Layout #{layout}.#{format} already exists. Skipping..."
return
else
layout_file = File.expand_path("../templates/layout.#{format}.xqy", __FILE__)
layout_file = File.expand_path("../templates/layout.xqy", __FILE__) unless File.exists?(layout_file)
end

File.open(target_file, 'a') {|f| f.write(File.read(layout_file)) }
end


def create_controller(controller, view)
target_file = File.expand_path("#{@src_dir}/app/controllers/#{controller}.xqy", __FILE__)
controller_file = nil
if File.exists? target_file
existing = File.read(target_file)

if existing.index("c:#{view}()") != nil
logger.warn "function #{controller}:#{view}() already exists. Skipping..."
return
end
controller_file = File.read File.expand_path('../templates/controller-function.xqy', __FILE__)
else
controller_file = File.read File.expand_path('../templates/controller.xqy', __FILE__)
end
layout_file = @template_base + "/layout.#{format}.xqy"
layout_file = @template_base + "/layout.xqy" unless file_exists?(layout_file)

controller_file.gsub!("#controller-name", controller)
controller_file.gsub!("#function-name", view)
File.open(target_file, 'a') {|f| f.write(controller_file) }
File.open(target_file, 'a') {|f| f.write(read_file(layout_file)) }
end

def create_view(controller, view, format)
Expand All @@ -183,11 +153,11 @@ def create_view(controller, view, format)

out_file = "#{dir}/#{view}.#{format}.xqy"

template_file = File.expand_path("../templates/view.#{format}.xqy", __FILE__)
template_file = File.expand_path("../templates/view.xqy", __FILE__) unless File.exists?(template_file)
template_file = @template_base + "/view.#{format}.xqy"
template_file = @template_base + "/view.xqy" unless file_exists?(template_file)

if File.exists?(out_file) == false
view_template = File.read template_file
view_template = read_file(template_file)
view_template.gsub!("#location", out_file)
view_template.gsub!("#controller", controller)
view_template.gsub!("#view", view)
Expand Down
7 changes: 6 additions & 1 deletion deploy/lib/ml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
require 'server_config'
require 'framework'
require 'util'
require 'app_specific'
require 'upgrader'
require 'scaffold'

if is_jar?
require ServerConfig.expand_path("./deploy/app_specific")
else
require 'app_specific'
end

def need_help?
find_arg(['-h', '--help']) != nil
end
Expand Down
54 changes: 38 additions & 16 deletions deploy/lib/server_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def vars
class ServerConfig < MLClient

# needed to determine if Roxy is running inside a jar
@@is_jar = __FILE__.match(/jar:file:.*/) != nil
@@is_jar = is_jar?
@@path = @@is_jar ? "./deploy" : "../.."
@@context = @@is_jar ? Dir.pwd : __FILE__

Expand Down Expand Up @@ -103,6 +103,8 @@ def info
end
end

# This method exists to return a path relative to roxy
# when running as regular old Roxy or as a jar
def ServerConfig.expand_path(path)
# logger.info("path: #{path}")
# logger.info("context: #{@@context}")
Expand All @@ -123,13 +125,16 @@ def self.jar
Dir.mktmpdir do |tmp_dir|
logger.debug(tmp_dir)

FileUtils.mkdir_p tmp_dir + "/bin"
FileUtils.cp(ServerConfig.expand_path("#{@@path}/lib/ml.rb"), tmp_dir + "/bin/roxy.rb")
temp_roxy_dir = tmp_dir + "/roxy"
Dir.mkdir temp_roxy_dir

FileUtils.cp_r(ServerConfig.expand_path("#{@@path}/lib"), tmp_dir)
FileUtils.cp(ServerConfig.expand_path("#{@@path}/app_specific.rb"), tmp_dir + "/lib/app_specific.rb")
FileUtils.mkdir_p temp_roxy_dir + "/bin"
FileUtils.cp(ServerConfig.expand_path("#{@@path}/lib/ml.rb"), temp_roxy_dir + "/bin/roxy.rb")

Dir.chdir(tmp_dir) do
FileUtils.cp_r(ServerConfig.expand_path("#{@@path}/lib"), temp_roxy_dir)
FileUtils.cp_r(ServerConfig.expand_path("#{@@path}/sample"), temp_roxy_dir)

Dir.chdir(temp_roxy_dir) do
Warbler::Application.new.run
FileUtils.cp(Dir.glob("*.jar")[0], jar_file)
end
Expand All @@ -140,15 +145,30 @@ def self.jar
end

def self.init
# input files
if @@is_jar
sample_config = "roxy/sample/ml-config.sample.xml"
sample_properties = "roxy/sample/build.sample.properties"
sample_options = "roxy/sample/all.sample.xml"
sample_rest_properties = "roxy/sample/properties.sample.xml"
else
sample_config = ServerConfig.expand_path("#{@@path}/sample/ml-config.sample.xml")
sample_properties = ServerConfig.expand_path("#{@@path}/sample/build.sample.properties")
sample_options = ServerConfig.expand_path("#{@@path}/sample/all.sample.xml")
sample_rest_properties = ServerConfig.expand_path("#{@@path}/sample/properties.sample.xml")
end

# output files
build_properties = ServerConfig.expand_path("#{@@path}/build.properties")
options_dir = ServerConfig.expand_path("#{@@path}/../rest-api/config/options")
options_file = ServerConfig.expand_path("#{@@path}/../rest-api/config/options/all.xml")
rest_properties = ServerConfig.expand_path("#{@@path}/../rest-api/config/properties.xml")

# dirs to create
rest_ext_dir = ServerConfig.expand_path("#{@@path}/../rest-api/ext")
rest_transforms_dir = ServerConfig.expand_path("#{@@path}/../rest-api/transforms")
options_file = ServerConfig.expand_path("#{@@path}/../rest-api/config/options/all.xml")
sample_options = ServerConfig.expand_path("#{@@path}/sample/all.sample.xml")
options_dir = ServerConfig.expand_path("#{@@path}/../rest-api/config/options")

# get supplied options
force = find_arg(['--force']).present?
force_props = find_arg(['--force-properties']).present?
force_config = find_arg(['--force-config']).present?
Expand All @@ -165,7 +185,7 @@ def self.init
error_msg << "build.properties has already been created."
else
#create clean properties file
FileUtils.cp sample_properties, build_properties
copy_file sample_properties, build_properties

properties_file = File.read(build_properties)

Expand Down Expand Up @@ -206,10 +226,8 @@ def self.init
FileUtils.mkdir_p rest_ext_dir
FileUtils.mkdir_p rest_transforms_dir
FileUtils.mkdir_p options_dir
FileUtils.cp sample_options, options_file
FileUtils.cp(
ServerConfig.expand_path("#{@@path}/sample/properties.sample.xml"),
ServerConfig.expand_path("#{@@path}/../rest-api/config/properties.xml"))
copy_file sample_options, options_file
copy_file sample_rest_properties, rest_properties
end

target_config = ServerConfig.expand_path(ServerConfig.properties["ml.config.file"])
Expand All @@ -218,21 +236,25 @@ def self.init
error_msg << "ml-config.xml has already been created."
else
#create clean marklogic configuration file
FileUtils.cp sample_config, target_config
copy_file sample_config, target_config
end

raise HelpException.new("init", error_msg.join("\n")) if error_msg.length > 0
end

def self.initcpf
if @@is_jar
sample_config = "roxy/sample/pipeline-config.sample.xml"
else
sample_config = ServerConfig.expand_path("#{@@path}/sample/pipeline-config.sample.xml")
end
target_config = ServerConfig.expand_path("#{@@path}/pipeline-config.xml")

force = find_arg(['--force']).present?
if !force && File.exists?(target_config)
raise HelpException.new("initcpf", "cpf configuration has already been created.")
else
FileUtils.cp sample_config, target_config
copy_file sample_config, target_config
end
end

Expand Down
Loading

0 comments on commit eca8ff3

Please sign in to comment.