How to copy files based on environment on gulp - angularjs

I have folder with files for different build environment like production,stage, and test.
example:
src/config.prod.js
src/config.stage.js
src/config.test.js
what I want is to copy config file based on environment I got, for getting environment name from command i using following code:
var nopt = require('nopt')
, knownOpts = {
"env" : [String, null]
}
, shortHands = {
"test" : ["--env", "test"]
, "dev" : ["--dev", "dev"]
, "stage" : ["--env", "stage"]
, "prod" : ["--env", "prod"]
};
var flags = nopt(knownOpts, shortHands, process.argv, 2);
and when I hit command
gulp build --env dev
I am getting the environment name, now what I want is to copy config file to dist folder(build) based on environment. I have this task for copy file but it copy all files as I don't know how to filter it out.
gulp.task('copyConfig', function(){
gulp.src(['src/*.js'])
.pipe(gulp.dest('dist/'))
})
I am new to gulp, if some one has any suggestion. please help.

new configs directory where you mv your 3, existing configs (prod, stage, test) ... mkdir configs
Then , 2 related changes to gulp to incorporate build.ENV and a copy.config task that knows about your 3 diff config files...
var settings = {
/*
* Environment development | production
* match './configs' files of same name
*/
environment : process.env.NODE_ENV || 'development',
/*
* Where is our config folder?
*/
configFolder : 'configs',
/*
* Where is our code?
*/
srcFolder : 'app/scripts',
/*
* Where are we building to?
*/
buildFolder : 'dist',
};
/**
* Config Task
*
* Get the configuration file (dev or prod), rename it
* and move it to be built.
*/
gulp.task('config', function() {
return gulp.src(settings.configFolder + '/' + settings.environment + '.js')
.pipe(rename('config.js'))
.pipe(gulp.dest(settings.srcFolder));
});

Related

How to add options to ntpd

I'd like to add a new option to ntpd however I couldn't find how to generate ntpd/ntpd-opts{.c, .h} after adding some lines to ntpd/ntpdbase-opts.def e.g.,
$ git diff ntpd/ntpdbase-opts.def
diff --git a/ntpd/ntpdbase-opts.def b/ntpd/ntpdbase-opts.def
index 66b953528..a790cbd51 100644
--- a/ntpd/ntpdbase-opts.def
+++ b/ntpd/ntpdbase-opts.def
## -479,3 +479,13 ## flag = {
the server to be discovered via mDNS client lookup.
_EndOfDoc_;
};
+
+flag = {
+ name = foo;
+ value = F;
+ arg-type = number;
+ descrip = "Some new option";
+ doc = <<- _EndOfDoc_
+ For testing purpose only.
+ _EndOfDoc_;
+};
Do you have any ideas?
how to generate ntpd/ntpd-opts{.c, .h} after adding some lines to ntpd/ntpdbase-opts.def
It is just in build scripts. Just compile https://github.com/ntp-project/ntp/blob/master-no-authorname/INSTALL#L30 it normally and make will pick it up.
https://github.com/ntp-project/ntp/blob/master-no-authorname/ntpd/Makefile.am#L304
https://github.com/ntp-project/ntp/blob/master-no-authorname/ntpd/Makefile.am#L183
In addition to #KamilCuk's answer, we need to do the following to add custom options:
Edit *.def file
Run bootstrap script
Run configure script with --disable-local-libopts option
Run make
For example,
$ git diff ntpd/ntpdbase-opts.def
diff --git a/ntpd/ntpdbase-opts.def b/ntpd/ntpdbase-opts.def
index 66b953528..a790cbd51 100644
--- a/ntpd/ntpdbase-opts.def
+++ b/ntpd/ntpdbase-opts.def
## -479,3 +479,13 ## flag = {
the server to be discovered via mDNS client lookup.
_EndOfDoc_;
};
+
+flag = {
+ name = foo;
+ value = F;
+ arg-type = number;
+ descrip = "Some new option";
+ doc = <<- _EndOfDoc_
+ For testing purpose only.
+ _EndOfDoc_;
+};
This change yields:
$ ./ntpd --help
ntpd - NTP daemon program - Ver. 4.2.8p15
Usage: ntpd [ -<flag> [<val>] | --<name>[{=| }<val>] ]... \
[ <server1> ... <serverN> ]
Flg Arg Option-Name Description
-4 no ipv4 Force IPv4 DNS name resolution
- prohibits the option 'ipv6'
...
-F Num foo Some new option
opt version output version information and exit
-? no help display extended usage information and exit
-! no more-help extended usage information passed thru pager
Options are specified by doubled hyphens and their name or by a single
hyphen and the flag character.
...

Bump a specific version number on SVN using Grunt

I have an AngularJS module, built with Grunt. The version of this module is managed in the package.json file.
What I need to do
I need to create a grunt task to release the module when needed. Here what this task must do :
Perform a tag of the current module files on SVN (and it has to be SVN, not GIT).
Upgrade the version in package.json to a given version (for example, I will pass --version = X.Y.Z as option to the grunt task). I don't want a solution based only on "patch", "minor" or "major" upgrades.
Commit the change on the package.json file.
What I found so far
grunt-bump allows me to pass a specific version, using --setversion option. But it cannot commit the change on SVN, it's only working with GIT.
grunt-svn-bump allows me to commit on SVN, but I can't find a way to specify the next version. And it cannot perform the "tag" part.
grunt-svn-tag allows me to tag the files on SVN repository.
Do you know another grunt plugin that could fit ? Any help will be appreciated.
I was bored looking for an existing task that would fit, so I finally created it, based on the code of grunt-bump and grunt-svn-bump :
grunt.registerTask('custom_bump', 'Custom task for bumping version after a release', function(){
var semver = require('semver');
var shell = require('shelljs');
function shellRun( cmd ){
if (grunt.option('dryRun')) {
grunt.log.writeln('Command (not running because of dryRun option): ' + cmd);
} else {
grunt.verbose.writeln('Running: ' + cmd);
var result = shell.exec(cmd, {silent:true});
if (result.code !== 0) {
grunt.log.error('Error (' + result.code + ') ' + result.output);
}
}
}
// Options
var options = this.options({
filepath: 'package.json',
commit: true,
commitMessage : 'New version following a release'
});
// Recover the next version of the component
var nextVersion = grunt.option('nextVersion');
if( !nextVersion ){
grunt.fatal( 'Next version is not defined.', 3 );
}
else if( !semver.valid( nextVersion ) ){
grunt.warn( 'Next version is invalid.', 3 );
}
// Upgrade version into package.json
var filepath = options.filepath;
var file = grunt.file.readJSON( filepath );
var currentVersion = file.version;
if( semver.lte( nextVersion, currentVersion ) ){
grunt.warn( 'Next version is lesser or equal than current version.' );
}
file.version = nextVersion;
grunt.log.write( 'Bumping version in ' + filepath + ' from ' + currentVersion + ' to ' + nextVersion + '... ' );
grunt.file.write( filepath, JSON.stringify( file, null, 2 ) );
grunt.log.ok();
// Commit the changed package.json file
if( options.commit ){
var message =
grunt.log.write( 'Committing ' + filepath + '... ' );
shellRun( 'svn commit "' + filepath + '" -m "' + options.commitMessage + '"' );
grunt.log.ok();
}
// Update the config for next tasks
var configProperty = 'pkg';
grunt.log.write( 'Updating version in ' + configProperty + ' config... ' );
var config = grunt.config( configProperty );
if( config ){
config.version = nextVersion;
grunt.config( configProperty, config );
grunt.log.ok();
} else {
grunt.log.warn( "Cannot update pkg config !" );
}
grunt.log.ok( 'Version updated from ' + currentVersion + ' to ' + nextVersion + '.' );
});
My 'release' task uses grunt-svn-tag and my custom bump task.
grunt.initConfig({
// ...
svn_tag: {
current: {
options: {
tag: '<%= pkg.name %>-<%= pkg.version %>'
}
}
}
});
grunt.registerTask('release', [
'svn_tag:current',
'custom_bump'
]);
small disclaimer, I don't use SVN or Grunt, but GIT and Gulp, so I don't really know the syntaxes to any of those.
That being said, I would not put this in a grunt/gulp task, but I would create a NPM task to just run a small shell-script. npm release X.Y.Z
The shellscript could contain something like this:
#!/usr/bin/env bash
VERSION=$2
echo "gulp bump $VERSION"
gulp bump $VERSION
echo "staging package.json"
git add package.json
echo "commit release"
git commit -m "release $VERSION"
git tag -a "$VERSION" -m "release $VERSION"
git push origin --tags
Now I haven't tested this syntax, but something along these lines is how I would try it.

waf - build works, custom build targets fail

The waf command waf build shows compiler errors (if there are any) while waf debug or waf release does not and always fails, utilizing the following wscript file (or maybe the wscript file has some other shortcomings I am currently not aware of):
APPNAME = 'waftest'
VERSION = '0.0.1'
def configure(ctx):
ctx.load('compiler_c')
ctx.define('VERSION', VERSION)
ctx.define('GETTEXT_PACKAGE', APPNAME)
ctx.check_cfg(atleast_pkgconfig_version='0.1.1')
ctx.check_cfg(package='glib-2.0', uselib_store='GLIB', args=['--cflags', '--libs'], mandatory=True)
ctx.check_cfg(package='gobject-2.0', uselib_store='GOBJECT', args=['--cflags', '--libs'], mandatory=True)
ctx.check_cfg(package='gtk+-3.0', uselib_store='GTK3', args=['--cflags', '--libs'], mandatory=True)
ctx.check_cfg(package='libxml-2.0', uselib_store='XML', args=['--cflags', '--libs'], mandatory=True)
ctx.check_large_file(mandatory=False)
ctx.check_endianness(mandatory=False)
ctx.check_inline(mandatory=False)
ctx.setenv('debug')
ctx.env.CFLAGS = ['-g', '-Wall']
ctx.define('DEBUG',1)
ctx.setenv('release')
ctx.env.CFLAGS = ['-O2', '-Wall']
ctx.define('RELEASE',1)
def pre(ctx):
print ('Building [[[' + ctx.variant + ']]] ...')
def post(ctx):
print ('Building is complete.')
def build(ctx):
ctx.add_pre_fun(pre)
ctx.add_post_fun(post)
# if not ctx.variant:
# ctx.fatal('Do "waf debug" or "waf release"')
exe = ctx.program(
features = ['c', 'cprogram'],
target = APPNAME+'.bin',
source = ctx.path.ant_glob(['src/*.c']),
includes = ['src/'],
export_includes = ['src/'],
uselib = 'GOBJECT GLIB GTK3 XML'
)
# for item in exe.includes:
# print(item)
from waflib.Build import BuildContext
class release(BuildContext):
cmd = 'release'
variant = 'release'
class debug(BuildContext):
cmd = 'debug'
variant = 'debug'
Error resulting from waf debug :
Build failed
-> task in 'waftest.bin' failed (exit status -1):
{task 46697488: c qqq.c -> qqq.c.1.o}
[useless filepaths]
I had a look at the waf demos, read the wafbook at section 6.2.2 but those did not supply me with valuable information in order to fix this issue.
What's wrong, and how do I fix it?
You need to do at least the following:
def configure(ctx):
...
ctx.setenv('debug')
ctx.load('compiler_c')
...
Since the cfg.setenv function resets whole previous environment. If you want to save previous environment, you can do cfg.setenv('debug', env=cfg.env.derive()).
Also, you don't need to explicitly specify the features = ['c', 'cprogram'], since, it's redundant, when you call bld.program(...).
P.S. Don't forget to reconfigure after modifying wscript file.

groovy copy files with same last modified date

Hi I want to copy a file from 1 directory to another, but the date has to be the same. so when the last modified date in the fromdirectory is 14:35, I want it to be the same in the todirectory.
How can I do this using groovy?
Using AntBuilder
new AntBuilder().copy ( file : 'path/to/source',
tofile : 'path/to/destination',
preservelastmodified : 'true' )
Using Java/Groovy File API
def source = new File ('path/to/source')
def destination = new File ('path/to/destination')
source.withInputStream { is ->
destination << is
}
destination.lastModified = source.lastModified()

Groovy - create file issue: The filename, directory name or volume label syntax is incorrect

I'm running a script made in Groovy from Soap UI and the script needs to generate lots of files.
Those files have also in the name two numbers from a list (all the combinations in that list are different), and there are 1303 combinations
available and the script generates just 1235 files.
A part of the code is:
filename = groovyUtils.projectPath + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
targetFile.createNewFile();
where $file is actually that part of the file name which include those 2 combinations from that list:
file = "abc" + "-$firstNumer"+"_$secondNumber"
For those file which are not created is a message returned:"The filename, directory name or volume label syntax is incorrect".
I've tried puting another path:
filename = "D:\\rez\\" + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
targetFile.createNewFile();
and also:
File parentFolder = new File("D:\\rez\\");
File targetFile = new File(parentFolder, "$file"+"_OK.txt");
targetFile.createNewFile();
(which I've found here: What are possible reasons for java.io.IOException: "The filename, directory name, or volume label syntax is incorrect")
but nothing worked.
I have no ideea where the problem is. Is strange that 1235 files are created ok, and the rest of them, 68 aren't created at all.
Thanks,
My guess is that some of the files have illegal characters in their paths. Exactly which characters are illegal is platform specific, e.g. on Windows they are
\ / : * ? " < > |
Why don't you log the full path of the file before targetFile.createNewFile(); is called and also log whether this method succeeded or not, e.g.
filename = groovyUtils.projectPath + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
println "attempting to create file: $targetFile"
if (targetFile.createNewFile()) {
println "Successfully created file $targetFile"
} else {
println "Failed to create file $targetFile"
}
When the process is finished, check the logs and I suspect you'll see a common pattern in the ""Failed to create file...." messages
File.createNewFile() returns false when a file or directory with that name already exists. In all other failure cases (security, I/O) it throws an exception.
Evaluate createNewFile()'s return value or, additionally, use the File.exists() method:
File file = new File("foo")
// works the first time
createNewFile(file)
// prints an error message
createNewFile(file)
void createNewFile(File file) {
if (!file.createNewFile()) {
assert file.exists()
println file.getPath() + " already exists."
}
}

Resources