Skip to content

Commit

Permalink
Merge branch 'build_script'
Browse files Browse the repository at this point in the history
  • Loading branch information
ma4nn committed Apr 11, 2024
2 parents 8747396 + 099f2e4 commit ebd4884
Show file tree
Hide file tree
Showing 7 changed files with 687 additions and 348 deletions.
11 changes: 2 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
.DEFAULT_GOAL := dist
INSTALL_DIR = ~/Library/Containers/com.moneymoney-app.retail/Data/Library/Application\ Support/MoneyMoney/Extensions
OUTPUT_FILE = dist/SankeyChart.lua

.PHONY: dist
dist: clean
npm install
npx tsc
VERSION_SEMVER=`cat package.json | jq -r '.version'` && export VERSION=$${VERSION_SEMVER%.*} && \
export INLINE_JS=`npm run build:js --silent` && \
[ "$${INLINE_JS}" ] || exit 1 && \
export INLINE_CSS=`npm run build:css --silent` && \
[ "$${INLINE_CSS}" ] || exit 1 && \
envsubst '$$VERSION,$$INLINE_CSS,$$INLINE_JS' < src/SankeyChart.lua > $(OUTPUT_FILE) && \
chmod +x $(OUTPUT_FILE) && \
make distclean
npm run build

.PHONY: test
test:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ For more information see also [my blog post](https://dev-investor.de/finanz-apps
## Developing ⚒️

```
make dist && make test
make && make test
```

The compiled MoneyMoney extension can then be found in `dist/SankeyChart.lua` and installed with `make install`.
Expand Down
46 changes: 46 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const replace = require('replace-in-file');
const crypto = require('crypto');
const pkg = require('./package.json');
const pkgLock = require('./package-lock.json');

simplifyVersion = version => version.replace(/^[=<>~^]/g, '');
majorMinorVersion = version => version.split('.').slice(0, -1).join('.');

const urls = {
'bootstrap_js': 'https://cdn.jsdelivr.net/npm/bootstrap@' + simplifyVersion(pkgLock.packages["node_modules/bootstrap"].version) + '/dist/js/bootstrap.bundle.min.js',
'bootstrap_css': 'https://cdn.jsdelivr.net/npm/bootstrap@' + simplifyVersion(pkgLock.packages["node_modules/bootstrap"].version) + '/dist/css/bootstrap.min.css',
'highcharts_js': 'https://cdn.jsdelivr.net/npm/highcharts@' + simplifyVersion(pkgLock.packages["node_modules/highcharts"].version) + '/highcharts.js',
'highcharts_sankey_js': 'https://cdn.jsdelivr.net/npm/highcharts@' + simplifyVersion(pkgLock.packages["node_modules/highcharts"].version) + '/modules/sankey.js',
'highcharts_css': 'https://cdn.jsdelivr.net/npm/highcharts@' + simplifyVersion(pkgLock.packages["node_modules/highcharts"].version) + '/css/highcharts.css',
};

async function calculateSri(url) {
return fetch(url)
.then(response => response.text())
.then(data => {
const hash = crypto.createHash('sha384').update(data, 'utf8');
const hashBase64 = hash.digest('base64');
return 'sha384-' + hashBase64;
});
}

async function calculateSris(urls) {
return await Promise.all(urls.map(calculateSri));
}

(async() => {
const sris = await calculateSris(Object.values(urls));
// variable format {{ x }} has been chosen so that a JavaScript error is thrown in case that variable has not been replaced
let variables = [/{{ version }}/g, /{{ inline_css }}/g, /{{ inline_js }}/g];
Object.keys(urls).forEach(key => variables.push(new RegExp('{{ ' + key + '_url }}',"g")));
Object.keys(urls).forEach(key => variables.push(new RegExp('{{ ' + key + '_sri }}',"g")));

return {
files: pkg.config.outputDir + "/*.lua", // @todo read from stdin?
from: variables,
to: [majorMinorVersion(process.env.npm_package_version), process.env.INLINE_CSS, process.env.INLINE_JS, ...Object.values(urls), ...sris],
countMatches: true,
};
})().then(options => replace(options)
.then(results => results.every(result => (! result.hasChanged || result.numReplacements !== options.from.length) && (() => { throw new Error('error during build: no replacements done in file ' + result.file) })()))
);
Loading

0 comments on commit ebd4884

Please sign in to comment.