Last active
May 7, 2019 00:06
-
-
Save patrickarlt/906a47f0c67e420a09177e32728fc32c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the acetate extension | |
function gitCloneOrUpdate(config) { | |
return function(context) { | |
let cmd; | |
let repoPATH = path.join(config. config.gitCloneRootDirectory, config.gitCloneDirectory); | |
if (!fs.existsSync(repoPATH)) { | |
cmd = `git -C ${context.gitCloneRootDirectory} clone ${ | |
context.gitURL | |
} --depth 1 ${context.gitCloneRootDirectory} -q`; | |
} else { | |
cmd = `git -C ${repoPATH} fetch origin --depth 1 -q && git -C ${repoPATH} checkout FETCH_HEAD -q`; | |
} | |
try { execSync(cmd); } catch (e) { console.log(e.stack); } | |
} | |
} | |
function generateFromExternalGitRepo(options) { | |
// clone the repo ect... | |
gitCloneOrUpdate(options); | |
// return the aceate config to use the cloned content | |
return function (acetate) { | |
acetate.generate(options.generate) | |
// run any additional config, this could even be ANOTHER extension | |
options.config(acetate); | |
} | |
} | |
// in our main acetate config | |
acetate.use(generateFromExternalGitRepo({ | |
repo: ".net samples git url", | |
cloneLocation: ".net samples temp folder", | |
determinePageSrc: function (pageMetadata) { | |
return (metadata) => { | |
// metadata.templatePath is the original path to the readme in the repo | |
return determinePageSrc(metadata.title, metadata.templatePath); // /net/latest/{switcherKey}/sample-code/{title} | |
} | |
}, | |
config: function (acetate) { | |
// add additional arbitrary acetate config we could create seperate extensions | |
// for image processing for the runtime samples, extracting snippets ect... | |
} | |
})) | |
// in our main acetate config | |
function determineRuntimeSampleSrc(options) { | |
return (metadata) => { | |
const slug = makeSlug(metadata.title); | |
// metadata.templatePath is the original path to the readme in the repo | |
return `${options.sdk}/latest/${options.platform}/sample-code/${slug}` | |
} | |
} | |
function configureRuntimeSamples (options) { | |
// add additional arbitrary acetate config we could create seperate extensions | |
// for image processing for the runtime samples, extracting snippets ect... | |
return function (acetate) { | |
// build the nav | |
acetate.query(options.id, options.glob, (page)=> { | |
return { | |
title: page.title, | |
url: page.url, | |
category: page.category | |
} | |
}, (nav, page) => { | |
// add page to nav, sort all categories + page category | |
return nav; | |
}, []); | |
// assign the nav to each page | |
acetate.metadata(options.glob, { | |
nav: options.id | |
}) | |
// read snippets in prerender | |
acetate.prerender(options.glob, (page, callback) => { | |
page.snippetContent = page.snippets.map((snippet) => { | |
// map snippet content, do processing ect... | |
}) | |
callback(null, page); | |
}); | |
// copy images to build folder | |
acetate.prerender(options.glob, (page, callback) => { | |
page.images.forEach(image => { | |
const imageOutputPath = path.join(acetate.dest, path.resolve(page.dest, image)); | |
// copy image to imageOutputPath | |
// fsExtra.copy should make all the directories | |
}); | |
callback(null, page); | |
}); | |
} | |
} | |
function generateRuntimeSamples (options) { | |
acetate.use(generateFromExternalGitRepo({ | |
repo: options.repo, | |
cloneLocation: options.cloneLocation, | |
determinePageSrc: determineRuntimeSampleSrc(options), | |
pageGlob: options.pageGlob, | |
pageMetadata: { | |
// custom page metadata from options or directly defined | |
}, | |
sdk: options.sdk, | |
platform: options.platform, | |
config: configureRuntimeSamples(options), | |
generate: (createPage, cb) => { | |
glob(path.join(options.cloneLocation, options.pageGlob)).then(readmePaths => { | |
const pages = readmePaths.map(readmePath => { | |
// I need to add functionality to acetate to allow dynamically | |
// calculating the src from the metadata when the page gets built | |
// but this would create a new page object with the proper | |
// this also saves the original path of the readme file as readmePath | |
// in the metadata | |
const page = createPage.fromTemplate(options.determinePageSrc, readmePath); | |
}) | |
// generate the redirects from each page | |
const redirects = pages.reduce(((redirects,page) => { | |
const redirectsForPage = page.redirects.map((oldUrl) => { | |
const TEMPLATE = `<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Redirect</title> | |
<meta charset="utf-8"/> | |
<meta http-equiv="refresh" content="0; URL={{ redirectTo }}"> | |
</head> | |
<body> | |
</body> | |
</html>`; | |
return createPage(oldUrl, TEMPLATE, { | |
prettyUrl: false, | |
redirectTo: page.url, | |
layout: false, | |
sitemap: false | |
}); | |
}); | |
return redirects.concat(redirectsForPage); | |
}), []) | |
cb(null, pages.concat(redirects)); | |
}) | |
} | |
})) | |
} | |
// run this for each sdk + platform combo... | |
generateRuntimeSamples({ | |
repo: "qt samples git url", | |
cloneLocation: "qt samples temp folder", | |
pageGlob: "ArcGISRuntimeSDKQt_QMLSamples/**/README.md", | |
sdk: "qt", | |
platform: "qml" | |
}) | |
acetate.use(generateFromExternalGitRepo({ | |
repo: "python samples git url", | |
cloneLocation: "python samples temp folder", | |
pageGlob: "**/*.pynb", | |
sdk: "python", | |
platform: "", | |
generate: () => {}, | |
config: () => {}, | |
determinePageSrc: () => {}, | |
pageMetadata: { | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment