Skip to content

Instantly share code, notes, and snippets.

@TBXark
Last active April 28, 2022 08:31
Show Gist options
  • Save TBXark/c8b105147bea3a2f0fb3ad0090ee1571 to your computer and use it in GitHub Desktop.
Save TBXark/c8b105147bea3a2f0fb3ad0090ee1571 to your computer and use it in GitHub Desktop.
/// https://developers.cloudflare.com/workers/
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// https://example.com/author/repo/name.podspec?xxx=xxx
const url = new URL(request.url)
const [, author, repo, name] = url.pathname.split(/[\./]/g)
// version=1.0.0
const version = url.searchParams.get('version')
// dependencies=A,B,C
const dependencies = url.searchParams.get('dependencies')?.split(',') ?? []
// subspecs=core!dep1,dep2$ui!core,dep3
const subspecs = url.searchParams.get('subspecs')?.split('$') ?? []
// default_subspec=default
const defaultSubspec = url.searchParams.get('default_subspec')
// target=ios!9.0$osx!10.9$tvos!9.0$watchos!3.0
const target = url.searchParams.get('target')?.split('$') ?? ['ios!9.0$osx!10.9$tvos!9.0$watchos!3.0']
return new Response(createPodspec(name, repo, author, version, dependencies, subspecs, defaultSubspec, target))
}
function createPodspec(name, repo, author, version, dependencies, subspecs, defaultSubspec, target) {
return `
Pod::Spec.new do |s|
s.name = '${name}'
s.version = '${version}'
s.summary = 'SPM to CocoaPods Bridge.'
s.description = 'Swift Package Manager to CocoaPods Bridge.'
s.homepage = 'https://github.com/${author}/${repo}'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '${author}' => 'letpods@tbxark.com' }
s.source = { :git => 'https://github.com/${author}/${repo}.git', :tag => '${version}' }
${
target.length > 0 ? target.map(t => {
const [name, version] = t.split('!')
return ` s.${name}.deployment_target = '${version}'`
} ).join('\n') : ''
}
${
defaultSubspec == null ? ` s.source_files = 'Sources/${name}/**/*'` : ''
}
${
dependencies.length > 0 ? dependencies.map(dependency => ` s.dependency '${dependency}'`).join('\n') : ''
}
${
subspecs.length > 0 ? subspecs.map(subspec => {
const [name, dependencies] = subspec.split('!')
return `
s.subspec '${name}' do |ss|
ss.source_files = 'Sources/${name}/**/*'
${
dependencies.length > 0 ? dependencies.map(dependency => ` ss.dependency '${dependency}'`).join('\n') : ''
}
end`
}).join('\n') : ''
}
${
defaultSubspec ? ` s.default_subspec = '${defaultSubspec}'` : ''
}
end
`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment