Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to keep original file permissions of template/sdk files #99

Merged
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions languages/c/language.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"/include/Common/Module.h",
"/src/Module_Common.cpp",
"/src/JsonData_Module.h"
]
}
],
"persistPermission": true
}
22 changes: 19 additions & 3 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { emptyDir, readDir, readFiles, readJson, writeFiles, writeText } from '../shared/filesystem.mjs'
import { emptyDir, readDir, readFiles, readFilesPermissions, readJson,
writeFiles, writeFilesPermissions, writeText } from '../shared/filesystem.mjs'
import { getTemplate, getTemplateForModule } from '../shared/template.mjs'
import { getModule, hasPublicAPIs } from '../shared/modules.mjs'
import { logHeader, logSuccess } from '../shared/io.mjs'
Expand All @@ -42,6 +43,7 @@ const macrofy = async (
staticContent,
templatesPerModule,
templatesPerSchema,
persistPermission,
createModuleDirectories,
copySchemasIntoModules,
aggregateFile,
Expand Down Expand Up @@ -87,6 +89,12 @@ const macrofy = async (
const sharedTemplateList = await readDir(sharedTemplates, { recursive: true })
const templates = Object.assign(await readFiles(sharedTemplateList, sharedTemplates),
await readFiles(sdkTemplateList, template)) // sdkTemplates are second so they win ties
let templatesPermission = {}
if (persistPermission) {
templatesPermission = Object.assign(await readFilesPermissions(sharedTemplateList, sharedTemplates),
await readFilesPermissions(sdkTemplateList, template))
}

const exampleTemplates = {}
for (var i=0; i<examples.length; i++) {
const example = examples[i]
Expand Down Expand Up @@ -135,9 +143,14 @@ const macrofy = async (

const content = engine.insertAggregateMacros(templates[file], aggregateMacros)
outputFiles[path.join(output, outputFile)] = content

if (persistPermission) {
templatesPermission[path.join(output, outputFile)] = templatesPermission[file]
}
logSuccess(`Generated macros for file ${path.relative(output, path.join(output, outputFile))}`)
}
if (persistPermission) {
delete templatesPermission[file]
}
})

let append = false
Expand Down Expand Up @@ -262,7 +275,10 @@ const macrofy = async (
await emptyDir(output)
}

await writeFiles(outputFiles)
await writeFiles(outputFiles)
if (persistPermission) {
await writeFilesPermissions(templatesPermission)
}
logSuccess(`Wrote ${Object.keys(outputFiles).length} files.`)

resolve()
Expand Down
3 changes: 2 additions & 1 deletion src/sdk/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const run = async ({
staticContent: path.join(language, 'src', 'shared'),
templatesPerModule: config.templatesPerModule,
templatesPerSchema: config.templatesPerSchema,
persistPermission: config.persistPermission,
operators: config.operators,
createModuleDirectories: config.createModuleDirectories,
copySchemasIntoModules: config.copySchemasIntoModules,
Expand All @@ -70,4 +71,4 @@ const run = async ({
})
}

export default run
export default run
63 changes: 42 additions & 21 deletions src/shared/filesystem.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import path from 'path'
import { readFile, writeFile, readdir } from 'fs/promises'
import { lstatSync } from 'fs'
import { readFile, writeFile, readdir, stat } from 'fs/promises'
import { lstatSync, chmodSync } from 'fs'
import { emptyDir } from 'fs-extra'
import { mkdirpSync as mkdirSync } from 'fs-extra'

const setPermission = (ref, mode) => chmodSync(ref, mode)

const readText = ref => readFile(ref)
.then(resp => resp.toString())

Expand Down Expand Up @@ -39,6 +41,34 @@ const readDir = async (ref, options) => {

return results.sort()
}
const getIndex = (refs, base) => {
let index = base ? base.length : 0
if (base && !refs[0].startsWith(base)) {
refs = refs.map(v => path.relative(base, v))
index = 0
}
else if (index === 0 && refs.length !== 1) {
// find the common prefix of all the files
while ((new Set(refs.map(r => r[index]))).size === 1) {
index++
}
// back up one dirctory from the common prefix
index = path.join(path.join(refs[0].substring(0, index)), '..').length
}
return index
}

const readFilesPermissions = (refs, base) => Promise.all(refs.map(ref => stat(ref)))
.then(permissions => {
if (!refs || refs.length === 0) {
return Promise.resolve({})
}
const results = refs.map(v => [v.substring(getIndex(refs, base)), null])
for (let i=0; i<refs.length; i++) {
results[i][1] = permissions[i].mode
}
return Promise.resolve(Object.fromEntries(results))
})

const binFormats = [ '.png', '.jpg', '.gif' ]

Expand All @@ -47,23 +77,7 @@ const readFiles = (refs, base) => Promise.all(refs.map(ref => readFile(ref)))
if (!refs || refs.length === 0) {
return Promise.resolve({})
}

let index = base ? base.length : 0
if (base && !refs[0].startsWith(base)) {
refs = refs.map(v => path.relative(base, v))
index = 0
}
else if (index === 0 && refs.length !== 1) {
// find the common prefix of all the files
while ((new Set(refs.map(r => r[index]))).size === 1) {
index++
}

// back up one dirctory from the common prefix
index = path.join(path.join(refs[0].substring(0, index)), '..').length
}

const results = refs.map(v => [v.substring(index), null])
const results = refs.map(v => [v.substring(getIndex(refs, base)), null])
for (let i=0; i<refs.length; i++) {
if (binFormats.find(suffix => refs[i].endsWith(suffix))) {
results[i][1] = contents[i]
Expand All @@ -83,6 +97,11 @@ const writeFiles = (files) => {
.map( ([file, contents]) => writeText(file, contents)))
}

const writeFilesPermissions = (files) => {
return Promise.all(Object.entries(files)
.map( ([file, mode]) => setPermission(file, mode)))
}

export {
readText,
writeText,
Expand All @@ -91,5 +110,7 @@ export {
readDir,
readFiles,
writeFiles,
emptyDir
}
emptyDir,
readFilesPermissions,
writeFilesPermissions
}
Loading