Skip to content

Commit

Permalink
BugZilla 518578 : Let users to use .Chefile in addition to Chefile (e…
Browse files Browse the repository at this point in the history
…clipse-che#5471)

* BugZilla 518578 : Let users to use .Chefile in addition to Chefile. It allows to use a hidden file format

Change-Id: Ie290a4550ccbdcc9aac64cb0c3bc60ac434a2ed8
Signed-off-by: Florent BENOIT <fbenoit@redhat.com>
  • Loading branch information
benoitf authored Jul 18, 2017
1 parent a162b38 commit 32d3545
Showing 1 changed file with 71 additions and 9 deletions.
80 changes: 71 additions & 9 deletions dockerfiles/lib/src/internal/dir/che-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ export class CheDir {
currentFolder: string = this.path.resolve('./');
folderName: any;
cheFile : any;

/**
* Alternate path to the Chefile file with .Chefile
*/
dotCheFile: string;

/**
* Alternate path to the Chefile file with .chefile
*/
dotCheFileLowercase: string;

dotCheFolder : any;
workspacesFolder : any;
cliFolder : any;
Expand Down Expand Up @@ -100,6 +111,8 @@ export class CheDir {
this.currentFolder = this.path.resolve(args[0]);
this.folderName = this.path.basename(this.currentFolder);
this.cheFile = this.path.resolve(this.currentFolder, 'Chefile');
this.dotCheFile = this.path.resolve(this.currentFolder, '.Chefile');
this.dotCheFileLowercase = this.path.resolve(this.currentFolder, '.chefile');
this.dotCheFolder = this.path.resolve(this.currentFolder, '.che');
this.dotCheIdFile = this.path.resolve(this.dotCheFolder, 'id');
this.dotCheSshPrivateKeyFile = this.path.resolve(this.dotCheFolder, 'ssh-key.private');
Expand Down Expand Up @@ -197,12 +210,16 @@ export class CheDir {

parse() {

try {
this.fs.statSync(this.cheFile);
// we have a file
} catch (e) {
let cheFiles : Array<any> = this.findCheFiles();

if (cheFiles.length === 0) {
Log.getLogger().debug('No chefile defined, use default settings');
return;
} else if (cheFiles.length >= 2) {
throw new Error('Error while parsing the Chefile as it was found at ' + JSON.stringify(cheFiles) + '. Only one file at a time is allowed.');
} else {
this.cheFile = cheFiles[0];
Log.getLogger().debug('Parsing chefile with name ', this.cheFile);
}

// load the chefile script if defined
Expand Down Expand Up @@ -366,25 +383,70 @@ export class CheDir {

}

/**
* Checks if the given file is existing or not
* @param filename the path to check
* @returns {boolean} true if it exists
*/
fileExistsCaseSensitive(filename) : boolean {
var dir = this.path.dirname(filename);
if (dir === '/' || dir === '.') {
return true;
}
var filenames = this.fs.readdirSync(dir);
if (filenames.indexOf(this.path.basename(filename)) === -1) {
return false;
}
return this.fileExistsCaseSensitive(dir);
}

/**
* Search all the chefiles with the corresponding pattern : Chefile, .Chefile, .chefile
* @returns {Array<any>} containing all references found
*/
findCheFiles() : Array<any> {

let foundCheFiles : Array<any> = new Array<any>();

// Try with Chefile
if (this.fileExistsCaseSensitive(this.cheFile)) {
Log.getLogger().debug('Chefile is present at ', this.cheFile);
foundCheFiles.push(this.cheFile);
}

// Try with .Chefile
if (this.fileExistsCaseSensitive(this.dotCheFile)) {
Log.getLogger().debug('The alternate file .Chefile is present at ', this.dotCheFile);
foundCheFiles.push(this.dotCheFile);
}

// Try with .chefile
if (this.fileExistsCaseSensitive(this.dotCheFileLowercase)) {
this.fs.statSync(this.dotCheFileLowercase);
Log.getLogger().debug('The alternate file .chefile is present at ', this.dotCheFileLowercase);
foundCheFiles.push(this.dotCheFileLowercase);
}
return foundCheFiles;
}


init() : Promise<any> {
return this.isInitialized().then((isInitialized) => {
if (isInitialized) {
Log.getLogger().warn('Che already initialized');
} else {
// needs to create folders
Log.getLogger().info('Adding', this.dotCheFolder, 'directory');
this.initCheFolders();

let cheFiles: Array<any> = this.findCheFiles();

// write a default chefile if there is none
try {
this.fs.statSync(this.cheFile);
Log.getLogger().debug('Chefile is present at ', this.cheFile);
} catch (e) {
if (cheFiles.length === 0) {
// write default
Log.getLogger().debug('Write a default Chefile at ', this.cheFile);
this.writeDefaultChefile();
}
Log.getLogger().info('Adding', this.dotCheFolder, 'directory');
return true;
}

Expand Down

0 comments on commit 32d3545

Please sign in to comment.