Skip to content

Commit

Permalink
docker builds and pre gyp
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Nov 6, 2021
1 parent 82fc5e3 commit ca150cb
Show file tree
Hide file tree
Showing 14 changed files with 2,856 additions and 109 deletions.
17 changes: 2 additions & 15 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
**
!ci
ci/coverage-report
!package.json
!binding.gyp
!lib
!install
!test
!cc
!data/got.jpg
!data/Lenna.png
!data/people.jpeg
!data/traffic.mp4
!data/text-models
!native-node-utils
build
node_modules
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM koush/node-opencv

WORKDIR /
COPY . opencv4nodejs

ARG NODE_PRE_GYP_GITHUB_TOKEN

WORKDIR /opencv4nodejs
RUN npm install
RUN npm run build
RUN npm run node-pre-gyp-publish
54 changes: 35 additions & 19 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
{
"targets": [{
"target_name": "opencv4nodejs",
"targets": [{
"target_name": "opencv4nodejs",
"defines": [
"<!@(node ./install/parseEnv.js OPENCV4NODEJS_DEFINES)",
],
"include_dirs" : [
"<!@(node ./install/parseEnv.js OPENCV4NODEJS_INCLUDES)",
"include_dirs": [
"cc",
"cc/core",
"<!(node -e \"require('nan')\")",
"<!(node -e \"require('native-node-utils')\")"
],
"libraries": [
"<!@(node ./install/parseEnv.js OPENCV4NODEJS_LIBRARIES)",
],
"sources": [
"cc/opencv4nodejs.cc",
Expand Down Expand Up @@ -116,30 +113,49 @@
"cc/xfeatures2d/SURFDetector.cc"
],

"cflags" : [
"cflags": [
"-std=c++17"
],
"cflags!" : [
"cflags!": [
"-fno-exceptions"
],
"cflags_cc!": [
"-fno-rtti",
"-fno-exceptions"
],
"ldflags" : [
"ldflags": [
"-Wl,-rpath,'$$ORIGIN'"
],
"xcode_settings": {
"OTHER_CFLAGS": [
"-std=c++17",
"-stdlib=libc++"
],
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
"MACOSX_DEPLOYMENT_TARGET": "11.0"
},


"conditions": [
[ "OS==\"win\"", {
['OS=="linux"', {
'cflags': [
'<!@(pkg-config --cflags opencv4)',
],
'cflags_cc': [
'<!@(pkg-config --cflags opencv4)',
],
'ldflags': [
'<!@(pkg-config --libs opencv4)',
],
}],
['OS=="mac"', {
"xcode_settings": {
"OTHER_CFLAGS": [
"-std=c++17",
"-stdlib=libc++",
'<!@(node ./install/pkg-config <!(pkg-config --cflags opencv))',
],
"OTHER_LDFLAGS": [
'<!@(node ./install/pkg-config <!(pkg-config --libs opencv))',
],
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
"MACOSX_DEPLOYMENT_TARGET": "11.0"
},
}],

["OS==\"win\"", {
"cflags": [
"-Wall"
],
Expand All @@ -161,7 +177,7 @@
"cflags": ["--coverage"],
"ldflags": ["--coverage"]
},
}
}

}]
}
3 changes: 3 additions & 0 deletions docker-buildx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# docker buildx create --name mybuild
# docker buildx use mybuild
docker buildx build --build-arg NODE_PRE_GYP_GITHUB_TOKEN=$GITHUB_TOKEN --platform linux/amd64,linux/arm64,linux/armhf .
23 changes: 0 additions & 23 deletions install/install.js

This file was deleted.

4 changes: 0 additions & 4 deletions install/node-gyp-darwin.sh

This file was deleted.

4 changes: 0 additions & 4 deletions install/node-gyp-linux.sh

This file was deleted.

9 changes: 0 additions & 9 deletions install/node-gyp.js

This file was deleted.

148 changes: 148 additions & 0 deletions install/node-pre-gyp-publish-github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
"use strict";

var path = require("path");
var fs = require('fs');
var mime = require("mime-types");
var cwd = process.cwd();

class NodePreGypGithub {
constructor() {
var ownerRepo, hostPrefix;

this.package_json = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json')));

if (!this.package_json.repository || !this.package_json.repository.url) {
throw new Error('Missing repository.url in package.json');
}
else {
ownerRepo = this.package_json.repository.url.match(/https?:\/\/([^\/]+)\/(.*)(?=\.git)/i);
if (ownerRepo) {
this.host = 'api.' + ownerRepo[1];
ownerRepo = ownerRepo[2].split('/');
this.owner = ownerRepo[0];
this.repo = ownerRepo[1];
}
else
throw new Error('A correctly formatted GitHub repository.url was not found within package.json');
}

hostPrefix = 'https://' + this.host + '/' + this.owner + '/' + this.repo + '/releases/download/';
if (!this.package_json.binary || 'object' !== typeof this.package_json.binary || 'string' !== typeof this.package_json.binary.host) {
throw new Error('Missing binary.host in package.json');
}
else if (this.package_json.binary.host.replace('https://', 'https://api.').substr(0, hostPrefix.length) !== hostPrefix) {
throw new Error('binary.host in package.json should begin with: "' + hostPrefix + '"');
}
const ocotkit = require("@octokit/rest");
this.octokit = new ocotkit.Octokit({
auth: this.authenticate_settings(),
baseUrl: 'https://' + this.host,
headers: {
"user-agent": (this.package_json.name) ? this.package_json.name : "node-pre-gyp-github"
}
});

}
authenticate_settings() {
var token = process.env.NODE_PRE_GYP_GITHUB_TOKEN;
if (!token)
throw new Error('NODE_PRE_GYP_GITHUB_TOKEN environment variable not found');
return token;
}
async createRelease(args) {
var options = {
'host': this.host,
'owner': this.owner,
'repo': this.repo,
'tag_name': this.package_json.version,
'target_commitish': 'master',
'name': 'v' + this.package_json.version,
'body': this.package_json.name + ' ' + this.package_json.version,
'draft': true,
'prerelease': false
};

Object.keys(args).forEach(function (key) {
if (args.hasOwnProperty(key) && options.hasOwnProperty(key)) {
options[key] = args[key];
}
});
console.log(this.owner, this.repo);
return this.octokit.repos.createRelease(options);
}
uploadAssets() {
var asset;
console.log("Stage directory path: " + path.join(this.stage_dir));
fs.readdir(path.join(this.stage_dir), async (err, files) => {
if (err)
throw err;

if (!files.length)
throw new Error('No files found within the stage directory: ' + this.stage_dir);

files.forEach(async (file) => {
if (this.release && this.release.assets) {
asset = this.release.assets.filter(function (element, index, array) {
return element.name === file;
});
if (asset.length) {
throw new Error("Staged file " + file + " found but it already exists in release " + this.release.tag_name + ". If you would like to replace it, you must first manually delete it within GitHub.");
}
}
console.log("Staged file " + file + " found. Proceeding to upload it.");

const full = path.join(this.stage_dir, file);
await this.octokit.repos.uploadReleaseAsset({
data: fs.readFileSync(full),
owner: this.owner,
release_id: this.release.id,
repo: this.repo,
name: file,
});
console.log('Staged file ' + file + ' saved to ' + this.owner + '/' + this.repo + ' release ' + this.release.tag_name + ' successfully.');
});
});
}
async publish(options) {
options = (typeof options === 'undefined') ? {} : options;
const data = await this.octokit.repos.listReleases({
'owner': this.owner,
'repo': this.repo
});

var release;
// when remote_path is set expect files to be in stage_dir / remote_path after substitution
if (this.package_json.binary.remote_path) {
options.tag_name = this.package_json.binary.remote_path.replace(/\{version\}/g, this.package_json.version);
this.stage_dir = path.join(this.stage_dir, options.tag_name);
} else {
// This is here for backwards compatibility for before binary.remote_path support was added in version 1.2.0.
options.tag_name = this.package_json.version;
}

console.log(options);

release = data.data.filter(function (element, index, array) {
return element.tag_name === options.tag_name;
});

if (release.length === 0) {
const release = await this.createRelease(options);
this.release = release.data;
console.log(this.release);
if (this.release.draft) {
console.log('Release ' + this.release.tag_name + " not found, so a draft release was created. YOU MUST MANUALLY PUBLISH THIS DRAFT WITHIN GITHUB FOR IT TO BE ACCESSIBLE.");
}
else {
console.log('Release ' + release.tag_name + " not found, so a new release was created and published.");
}
}
else {
this.release = release[0];
}

this.uploadAssets();
}
}
NodePreGypGithub.prototype.stage_dir = path.join(cwd, "build", "stage");
module.exports = NodePreGypGithub;
7 changes: 0 additions & 7 deletions install/parseEnv.js

This file was deleted.

5 changes: 5 additions & 0 deletions install/pkg-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { argv } = require("process");

for (let i = 2; i < argv.length; i++) {
console.log(argv[i])
}
30 changes: 30 additions & 0 deletions install/publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var module = require('./node-pre-gyp-publish-github');
var program = require('commander');

program
.command('publish [options]')
.description('publishes the contents of .\\build\\stage\\{version} to the current version\'s GitHub release')
.option("-r, --release", "publish immediately, do not create draft")
.option("-s, --silent", "turns verbose messages off")
.action(function(cmd, options){
var opts = {},
x = new module();
opts.draft = options.release ? false : true;
opts.verbose = options.silent ? false : true;
x.publish(opts);
});

program
.command('help','',{isDefault: true, noHelp: true})
.action(function() {
console.log();
console.log('Usage: node-pre-gyp-github publish');
console.log();
console.log('publishes the contents of .\\build\\stage\\{version} to the current version\'s GitHub release');
});

program.parse(process.argv);

if (!program.args.length) {
program.help();
}

0 comments on commit ca150cb

Please sign in to comment.