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

Fixed #377: Added support for deploy rest of .sjs, and some cleanup #385

Merged
merged 1 commit into from
Mar 10, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 123 additions & 52 deletions deploy/lib/ml_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(options)
def get_files(path, data = [])
@logger.debug "getting files for #{path}"
if (File.directory?(path))
Dir.glob("#{path}/*.{xqy,xslt,xsl}") do |entry|
Dir.glob("#{path}/*.{xqy,xqe,xq,xquery,xslt,xsl,sjs}") do |entry|
if File.directory?(entry)
get_files(entry, data)
else
Expand Down Expand Up @@ -64,51 +64,102 @@ def install_properties(path)

def install_extensions(path)
if (File.exists?(path))
headers = {
'Content-Type' => 'application/xquery'
}

data = get_files(path)
size = data.length

data.each_with_index do |d, i|
file = open(d, "rb")
contents = file.read
extensionName = $1 if contents =~ /module\s*namespace\s*[\w\-]+\s*=\s*"http:\/\/marklogic.com\/rest-api\/resource\/([^"]+)"/
params = []
contents.scan(/function\s+[^:]+:(get|put|post|delete)/).each do |m|
params << "method=#{m[0]}"
end

file_ext = File.extname(d)[1..-1]
file_name = File.basename(d, ".*")

is_sjs = (file_ext == "sjs")
is_xsl = file_ext.include?("xsl")
next if is_xsl # XSLT rest extension not supported

# look for annotations of this form:
# %roxy:params("argname=type", "anotherarg=type")
contents.scan(/declare\s+(\%\w+:\w+\(([\"\w\-\=\,\s:]*)\))*\s*function\s+[^:]+:(get|put|post|delete)/m).each do |m|
args = '';
verb = m[2]
if (m[0] && (m[0].include? "%roxy:params"))
if (m[1].match(/\"/))
m[1].gsub!(/\"/, '').split(',').each do |p|
arg = p.strip
parts = arg.split('=')
param = parts[0]
type = parts[1]
@logger.debug("param: #{param}")
@logger.debug("type: #{type}")
params << "#{verb}:#{param}=#{url_encode(type)}"
@logger.debug "Deploying #{File.basename(d)}"

headers = {
'Content-Type' => (is_sjs ? 'application/vnd.marklogic-javascript' : 'application/xquery')
}
params = []

extensionName = file_name

if (is_sjs)

contents.scan(/@name\s+(\b\w*\b)/).each do |m|
if (!m[0].nil? || !m[0].to_s.empty?)
extensionName = m[0]
end
end

contents.scan(/exports+[.]+(GET|PUT|POST|DELETE)/).each do |m|
params << "method=#{m[0].downcase}"
end

# look for annotations of this form:
# /**
# * @param {string} myString The string
# * @param {int} myInt The integer
# */
# exports.GET = get;
args = []
contents.scan(/exports+[.]+(GET|PUT|POST|DELETE)|@param\s+[\{]+(string|int)+[\}]\s+(\b\w*\b)/m).each do |m|
if (!m[1].nil? && !m[1].to_s.empty? && !m[2].nil? && !m[2].to_s.empty?)
args << ":#{m[2]}=xs:#{m[1]}"
end
if (!m[0].nil? || !m[0].to_s.empty?)
args.each do |arg|
params << "#{m[0].downcase}#{arg}"
end
args = []
end
end

else
# XQuery

extensionName = $1 if contents =~ /module\s*namespace\s*[\w\-]+\s*=\s*"http:\/\/marklogic.com\/rest-api\/resource\/([^"]+)"/

contents.scan(/function\s+[^:]+:(get|put|post|delete)/).each do |m|
params << "method=#{m[0]}"
end

# look for annotations of this form:
# %roxy:params("argname=type", "anotherarg=type")
contents.scan(/declare\s+(\%\w+:\w+\(([\"\w\-\=\,\s:?*+]*)\))*\s*function\s+[^:]+:(get|put|post|delete)/m).each do |m|
args = '';
verb = m[2]
if (m[0] && (m[0].include? "%roxy:params"))
if (m[1].match(/\"/))
m[1].gsub!(/\"/, '').split(',').each do |p|
arg = p.strip
parts = arg.split('=')
param = parts[0]
type = parts[1]
@logger.debug("param: #{param}")
@logger.debug("type: #{type}")
params << "#{verb}:#{param}=#{url_encode(type)}"
end
end
end
end

end

@logger.debug "extensionName: #{extensionName}"
@logger.debug "headers: #{headers}"
@logger.debug "params: #{params}"
# @logger.debug "methods: #{methods}"

url = "http://#{@hostname}:#{@port}/v1/config/resources/#{extensionName}"
if (params.length > 0)
url << "?" << params.join("&")
end
@logger.debug "loading: #{d}"

r = go(url, "put", headers, nil, contents)
if (r.code.to_i < 200 && r.code.to_i > 206)
@logger.error("code: #{r.code.to_i} body:#{r.body}")
Expand All @@ -122,37 +173,55 @@ def install_extensions(path)
def install_transforms(path)
if (File.exists?(path))


data = get_files(path)
size = data.length

data.each_with_index do |d, i|

@logger.debug "Deploying #{File.basename(d)}"

if (File.extname(d).include?("xq"))
file_type = 'xquery'
headers = {
'Content-Type' => 'application/xquery'
}
elsif (File.extname(d).include?("xsl"))
file_type = 'xslt'
headers = {
'Content-Type' => 'application/xslt+xml'
}
end


file = open(d, "rb")
contents = file.read
transformName = File.basename(d).gsub(/(.xqy|.xquery|.xq|.xslt|.xsl)$/, '')

file_ext = File.extname(d)[1..-1]
file_name = File.basename(d, ".*")

is_sjs = (file_ext == "sjs")
is_xsl = file_ext.include?("xsl")
is_xqy = file_ext.include?("xq")

headers = {
'Content-Type' => (is_sjs ? 'application/vnd.marklogic-javascript' : (is_xsl ? 'application/xslt+xml': 'application/xquery'))
}

transformName = file_name
params = []


# TODO: I'm assuming there's a way to consolidate the following if/elsif but I'm a Ruby newbie and I'm being conservative
if (file_type == 'xquery')

if (is_sjs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need a second look. Didn't have a .sjs transform at hand, not sure it works properly..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's working. Just tested it.


# look for annotations of this form:
# /**
# * @param {string} myString The string
# * @param {int} myInt The integer
# */
# exports.GET = get;
args = []
contents.scan(/exports+[.]+(GET|PUT|POST|DELETE)|@param\s+[\{]+([^\}]+)+[\}]\s+(\b\w*\b)/m).each do |m|
if (!m[1].nil? && !m[1].to_s.empty? && !m[2].nil? && !m[2].to_s.empty?)
args << ":#{m[2]}=xs:#{m[1]}"
end
if (!m[0].nil? || !m[0].to_s.empty?)
args.each do |arg|
params << "#{m[0].downcase}#{arg}"
end
args = []
end
end

elsif (is_xsl)

# look for annotations of this form:
# %roxy:params("argname=type", "anotherarg=type")
contents.scan(/declare\s+(\%\w+:\w+\(([\"\w\-\=\,\s:]*)\))*\s*function/m).each do |m|
contents.scan(/<!--\s*(\%\w+:\w+\(([\"\w\-\=\,\s:?*+]*)\))*\s*-->/m).each do |m|
args = '';
if (m[0] && (m[0].include? "%roxy:params"))
if (m[1].match(/\"/))
Expand All @@ -168,10 +237,12 @@ def install_transforms(path)
end
end
end
elsif (file_type == 'xslt')

else # XQuery

# look for annotations of this form:
# %roxy:params("argname=type", "anotherarg=type")
contents.scan(/<!--\s*(\%\w+:\w+\(([\"\w\-\=\,\s:]*)\))*\s*-->/m).each do |m|
contents.scan(/declare\s+(\%\w+:\w+\(([\"\w\-\=\,\s:?*+]*)\))*\s*function/m).each do |m|
args = '';
if (m[0] && (m[0].include? "%roxy:params"))
if (m[1].match(/\"/))
Expand All @@ -189,9 +260,9 @@ def install_transforms(path)
end
end

@logger.debug "params: #{params}"
@logger.debug "transformName: #{transformName}"
# @logger.debug "methods: #{methods}"
@logger.debug "headers: #{headers}"
@logger.debug "params: #{params}"
url = "http://#{@hostname}:#{@port}/v1/config/transforms/#{transformName}"
if (params.length > 0)
url << "?" << params.join("&")
Expand Down