import Foundation
let pattern = #".*\((.|\n)+\)\s*\{"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
# @summary Manages the puppeteer monitor service.
#
# @param logon_domain
# The service will logon using this domain. Specify '.' for a local account.
#
# @param logon_name
# The service will logon using this user name.
#
# @param logon_password
# The service will logon using this password.
#
# @param ensure
# Whether or not install the puppeteermon service.
#
# @param source
# Where to obtain the puppeteermon service executable from.
#
# @param state
# Ensure the puppeteermon service is in the given `state`.
#
# @param startup
# How to start the puppeteermon service.
#
# @param version
# The version of the puppeteermon service that should be installed.
#
class profile::ct::puppeteermon(
String $logon_domain = "${hiera('builder_domain', '.')}",
String $logon_name = "${hiera('builder_user', 'SYSTEM')}",
String $logon_password = "${hiera('builder_passwd', '')}",
String $cooldown = '10m',
String $min_exec_time = '1m15s',
Enum[absent, present] $ensure = present,
String $source = 'https://repo.ct.activision.com/repository/puppet/puppeteermon/<%= ::osfamily %>/puppeteermon-<%= version %>',
Enum['running', 'stopped'] $state = 'running',
Enum['automatic', 'manual', 'disabled']
$startup = 'automatic',
String $version = '2.0.11',
) {
include profile::ct::puppeteer
if $::osfamily == 'windows' {
# Non-configurable class parameters
$basedir = 'C:/ProgramData/puppeteermon'
$os_source = regsubst($source, '<%= ::osfamily %>', 'windows', 'G')
$puppeteer_os_source = regsubst($profile::ct::puppeteer::source, '<%= ::osfamily %>', 'windows', 'G')
$puppeteer_fetch_source = regsubst($puppeteer_os_source, '<%= version %>', "${profile::ct::puppeteer::version}.exe", 'G')
$puppeteer_recovery_source = regsubst($puppeteer_os_source, '<%= version %>', 'recovery.exe', 'G')
profile::ct::puppeteer::config { "${profile::ct::puppeteer::basedir}/puppeteer.conf":
ensure => $ensure,
compass_master_url => $profile::ct::puppeteer::compass_master_url,
runinterval => $profile::ct::puppeteer::runinterval,
splay => $profile::ct::puppeteer::splay,
version => $profile::ct::puppeteer::version,
}
-> profile::ct::puppeteermon::config { "${basedir}/puppeteermon.conf":
ensure => $ensure,
cooldown => $cooldown,
fetch_url => $puppeteer_fetch_source,
download_dir => "${profile::ct::puppeteer::basedir}/fetch",
command => "${profile::ct::puppeteer::basedir}/running/puppeteer.exe",
}
profile::ct::puppeteer::config_new { "${profile::ct::puppeteer::basedir}/puppeteer.yaml":
ensure => $ensure,
compass_master_url => $profile::ct::puppeteer::compass_master_url,
compass_polling_interval => $profile::ct::puppeteer::compass_polling_interval,
runinterval => $profile::ct::puppeteer::runinterval,
last_run_summary => $profile::ct::puppeteer::last_run_summary,
splay => $profile::ct::puppeteer::splay,
version => $profile::ct::puppeteer::version,
agent_config_platform => $profile::ct::puppeteer::agent_config_platform,
}
-> profile::ct::puppeteermon::config_new { "${basedir}/puppeteermon.yaml":
ensure => $ensure,
cooldown => $cooldown,
fetch_url => $puppeteer_fetch_source,
recovery_url => $puppeteer_recovery_source,
download_dir => "${profile::ct::puppeteer::basedir}/stage",
command => "${profile::ct::puppeteer::basedir}/running/puppeteer.exe",
min_exec_time => $min_exec_time
}
-> wservice { 'puppeteermon':
ensure => $ensure,
state => $state,
startup => $startup,
source => regsubst($os_source, '<%= version %>', "${version}.tar.gz", 'G'),
version => $version,
path => "${basedir}/puppeteermon.exe",
account => "${logon_domain}\\${logon_name}",
password => $logon_password,
registercmd => ['<%= path %>', 'install'],
unregistercmd => ['<%= path %>', 'uninstall'],
}
if $ensure == present {
$enable = $startup ? { 'automatic' => 'true', 'manual' => 'manual', default => 'false' }
service { 'puppeteermon':
ensure => $state,
enable => $enable,
require => Wservice['puppeteermon']
}
}
} elsif $::osfamily == 'Darwin' {
# Non-configurable class parameters
$basedir = '/opt/puppeteermon'
$os_source = regsubst($source, '<%= ::osfamily %>', 'darwin', 'G')
$puppeteer_os_source = regsubst($profile::ct::puppeteer::source, '<%= ::osfamily %>', 'darwin', 'G')
$puppeteer_fetch_source = regsubst($puppeteer_os_source, '<%= version %>', $profile::ct::puppeteer::version, 'G')
$puppeteer_recovery_source = regsubst($puppeteer_os_source, '<%= version %>', 'recovery', 'G')
profile::ct::puppeteer::config_new { "${profile::ct::puppeteer::basedir}/puppeteer.yaml":
ensure => $ensure,
compass_master_url => $profile::ct::puppeteer::compass_master_url,
compass_polling_interval => $profile::ct::puppeteer::compass_polling_interval,
runinterval => $profile::ct::puppeteer::runinterval,
last_run_summary => $profile::ct::puppeteer::last_run_summary,
splay => $profile::ct::puppeteer::splay,
version => $profile::ct::puppeteer::version,
agent_config_platform => $profile::ct::puppeteer::agent_config_platform,
}
-> profile::ct::puppeteermon::config_new { "${basedir}/puppeteermon.yaml":
ensure => $ensure,
cooldown => $cooldown,
fetch_url => $puppeteer_fetch_source,
recovery_url => $puppeteer_recovery_source,
download_dir => "${profile::ct::puppeteer::basedir}/stage",
command => "${profile::ct::puppeteer::basedir}/running/puppeteer",
min_exec_time => $min_exec_time
}
-> puppeteermon { '/opt/puppeteermon/puppeteermon':
ensure => $ensure,
version => $version,
source => regsubst($os_source, '<%= version %>', $version, 'G'),
}
if $ensure == present {
$enable = $startup ? {'automatic' => 'true', 'manual' => 'manual', default => 'false' }
service { 'puppeteermon':
ensure => $state,
enable => $enable,
provider => 'launchd',
require => Puppeteermon['/opt/puppeteermon/puppeteermon']
}
}
} else {
# Non-configurable class parameters
$basedir = '/opt/puppeteermon'
$os_source = regsubst($source, '<%= ::osfamily %>', 'linux', 'G')
$puppeteer_os_source = regsubst($profile::ct::puppeteer::source, '<%= ::osfamily %>', 'linux', 'G')
$puppeteer_fetch_source = regsubst($puppeteer_os_source, '<%= version %>', $profile::ct::puppeteer::version, 'G')
$puppeteer_recovery_source = regsubst($puppeteer_os_source, '<%= version %>', 'recovery', 'G')
profile::ct::puppeteer::config_new { "${profile::ct::puppeteer::basedir}/puppeteer.yaml":
ensure => $ensure,
compass_master_url => $profile::ct::puppeteer::compass_master_url,
compass_polling_interval => $profile::ct::puppeteer::compass_polling_interval,
runinterval => $profile::ct::puppeteer::runinterval,
last_run_summary => $profile::ct::puppeteer::last_run_summary,
splay => $profile::ct::puppeteer::splay,
version => $profile::ct::puppeteer::version,
agent_config_platform => $profile::ct::puppeteer::agent_config_platform,
}
-> profile::ct::puppeteermon::config_new { "${basedir}/puppeteermon.yaml":
ensure => $ensure,
cooldown => $cooldown,
fetch_url => $puppeteer_fetch_source,
recovery_url => $puppeteer_recovery_source,
download_dir => "${profile::ct::puppeteer::basedir}/stage",
command => "${profile::ct::puppeteer::basedir}/running/puppeteer",
min_exec_time => $min_exec_time
}
-> puppeteermon { '/opt/puppeteermon/puppeteermon':
ensure => $ensure,
version => $version,
source => regsubst($os_source, '<%= version %>', $version, 'G'),
}
if $ensure == present {
$enable = $startup ? {'automatic' => 'true', 'manual' => 'manual', default => 'false' }
service { 'puppeteermon':
ensure => $state,
enable => $enable,
provider => 'systemd',
require => Puppeteermon['/opt/puppeteermon/puppeteermon']
}
}
}
}
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression