diff --git a/README.md b/README.md index d66fbe2..4e2fef1 100644 --- a/README.md +++ b/README.md @@ -18,44 +18,20 @@ - :v: Automated dependency and package management - :hammer: Automatic import custom urls (e.g. jitpack) -### What is it? - -As an android developer I was jealous of the node.js community for their fast and reliable dependency managers, it's so easy to write `yarn add ` and a library is imported into the project... So I made `drone` for android! - -If you think about it there are like 50 libraries that are used in 95% of the apps (retrofit, rxjava, gson, picasso, roboletric, recyclerview-v7, etc.) - -I made this because every time I want to add (lets say) butterknife I need to -1. google butterknife -2. go to the readme -3. find the lines to copy -4. notice that butterknife has 2 dependencies -5. copy and paste in my gradle file -6. OPTIONAL if it was not in jcenter, go to the main build gradle and put the custom URL - -OR... - -```sh -drone add butterknife -``` -and everything will be done for you automatically - -## Install - -```sh -npm install -g drone -``` - ## Usage Simple usage: > drone add `` `` -------- + +Single library > drone add `picasso` app +Multiple > drone add `picasso` `retrofit` `rxjava` `gson` app

- +

``` @@ -89,6 +65,33 @@ Usage $ drone test picasso # Tests the library by fetching its version ``` +## Install + +```sh +npm install -g drone +``` + +### What is it? + +As an android developer I was jealous of the node.js community for their fast and reliable dependency managers, it's so easy to write `yarn add ` and a library is imported into the project... So I made `drone` for android! + +If you think about it there are like 50 libraries that are used in 95% of the apps (retrofit, rxjava, gson, picasso, roboletric, recyclerview-v7, etc.) + +I made this because every time I want to add (lets say) butterknife I need to +1. google butterknife +2. go to the readme +3. find the lines to copy +4. notice that butterknife has 2 dependencies +5. copy and paste in my gradle file +6. OPTIONAL if it was not in jcenter, go to the main build gradle and put the custom URL + +OR... + +```sh +drone add butterknife +``` +and everything will be done for you automatically + # Where are the libraries? Instead of maintaining a server with all the possible libraries I'm going with a [brew](https://brew.sh/) approach, the community will `create` a library `once` and it will be available to everyone else forever in the [hive](https://github.com/cesarferreira/drone-hive)! diff --git a/extras/add4.gif b/extras/add4.gif new file mode 100644 index 0000000..8914d65 Binary files /dev/null and b/extras/add4.gif differ diff --git a/src/tasks/add.js b/src/tasks/add.js index 19cb22a..3ae83f7 100644 --- a/src/tasks/add.js +++ b/src/tasks/add.js @@ -9,6 +9,7 @@ const Log = require('../utils/log_utils'); const Utils = require('../utils/utils'); const GradleUtils = require('../utils/gradle_utils'); const Constants = require('../utils/constants'); +const Install = require('../tasks/install'); function handle(found, info) { if (found.length > 0) { @@ -30,10 +31,8 @@ function handle(found, info) { function handleRepositoryInjection(info) { if (info.repository.url) { const mainGradleFilePath = GradleUtils.mainGradleFilePath(); - Utils.findStringInFile(info.repository.url, mainGradleFilePath) - .then(found => { - handle(found, info) - }); + const found = Utils.findStringInFileSync(info.repository.url, mainGradleFilePath) + handle(found, info) } } @@ -49,24 +48,22 @@ function findLineAndInsertDependency(module, dep, gradleFilePath) { } function injectDependency(dep, dependenciesLength, module, gradleFilePath) { - return Utils.findStringInFile(dep.dependency, gradleFilePath) - .then(found => { - if (found.length === 0) { - Log.title(`Inserted the following line`); - - const resultLine = findLineAndInsertDependency(module, dep, gradleFilePath); - log(Chalk.green(resultLine.trim())) - } else { - Log.titleError(`${Chalk.green(dep.dependency)} is already there @ line ${found[0].line}`) - } - }) - .catch(err => log(err)) + const found = Utils.findStringInFileSync(dep.dependency, gradleFilePath) + + if (found.length === 0) { + Log.title(`Inserted the following line`); + + const resultLine = findLineAndInsertDependency(module, dep, gradleFilePath); + log(Chalk.green(resultLine.trim())) + } else { + Log.titleError(`${Chalk.green(dep.dependency)} is already there @ line ${found[0].line}`) + } } -function handleGradleDependencyInjection(module, dependencies, gradleFilePath) { - if (GradleUtils.gradleFileExistsIn(module)) { +function handleGradleDependencyInjection(appModule, dependencies, gradleFilePath) { + if (GradleUtils.gradleFileExistsIn(appModule)) { var actions = dependencies.map(dep => { - return injectDependency(dep, dependencies.length, module, gradleFilePath); + return injectDependency(dep, dependencies.length, appModule, gradleFilePath); }) Promise.all(actions); @@ -76,6 +73,28 @@ function handleGradleDependencyInjection(module, dependencies, gradleFilePath) { } } +function handleSearchResponse(result, appModule) { + if (result.rating === 1) { + hive.getWithVersions(result.target) + .then(info => { + handleRepositoryInjection(info); + const gradlePath = GradleUtils.gradleFilePath(appModule); + handleGradleDependencyInjection(appModule, info.dependencies, gradlePath); + }); + } else { + Log.title('Did you mean'); + log(`${result.target}`); + } +} + +function handleInsertion(libraries, appModule) { + libraries.forEach(library => { + QuickSearch.search(library) + .then(result => { + handleSearchResponse(result, appModule); + }) + }) +} // Main code // const self = module.exports = { init: (input) => { @@ -84,23 +103,11 @@ const self = module.exports = { return; } - const module = input[input.length-1]; + const appModule = input[input.length-1]; const libraries = input.splice(0, input.length-1) - - libraries.forEach(library => { - QuickSearch.search(library) - .then(result => { - if (result.rating === 1) { - hive.getWithVersions(result.target) - .then(info => { - handleRepositoryInjection(info); - handleGradleDependencyInjection(module, info.dependencies, GradleUtils.gradleFilePath(module)); - }); - } else { - Log.title('Did you mean'); - log(`${result.target}`); - } - }); - }) - } - }; + + handleInsertion(libraries, appModule); + + // Install.downloadDependencies() + } +}; diff --git a/src/tasks/install.js b/src/tasks/install.js index 6ea64e1..fbbe110 100644 --- a/src/tasks/install.js +++ b/src/tasks/install.js @@ -11,18 +11,19 @@ const fs = require('fs'); // Main code // const self = module.exports = { - init: (input) => { + downloadDependencies: (input) => { let command = './gradlew dependencies' if (fs.existsSync('build.gradle')) { if (!fs.existsSync('gradlew')) { command = 'gradle dependencies' } Log.title(`Trying to install dependencies...`); - Utils.run(command) - .then(() => { - }) + Utils.run(command).then(() => {}); } else { Log.titleError(`This doesn't look like a gradle project`); } + }, + init: (input) => { + self.downloadDependencies(); } }; diff --git a/src/utils/gradle_utils.js b/src/utils/gradle_utils.js index d2b4a12..1017212 100644 --- a/src/utils/gradle_utils.js +++ b/src/utils/gradle_utils.js @@ -37,7 +37,7 @@ const self = module.exports = { return `${process.cwd()}/build.gradle`; }, mainGradleContentAsString: () => { - return fsp.readFile(self.mainGradleFilePath()); + return fsp.readFileSync(self.mainGradleFilePath()); }, gradleFilePath: module => { return `${process.cwd()}/${module}/build.gradle`; @@ -86,33 +86,26 @@ const self = module.exports = { return result.line - 1; }, findLineToInsertDependency: (repositoryInfo) => { - return self.mainGradleContentAsString() - .then(content => { - // find DEPENDENCY - return Utils.findStringInFile('repositories', self.mainGradleFilePath()) - .then(repositoriesFound => { - const cleanedRepositories = cleanupComments(repositoriesFound); + const content = self.mainGradleContentAsString() + // find DEPENDENCY + const repositoriesFound = Utils.findStringInFileSync('repositories', self.mainGradleFilePath()) + + const cleanedRepositories = cleanupComments(repositoriesFound); + const allProjectsFound = Utils.findStringInFileSync('allprojects', self.mainGradleFilePath()) + const cleanedAllProjects = cleanupComments(allProjectsFound); - return Utils.findStringInFile('allprojects', self.mainGradleFilePath()) - .then(allProjectsFound => { - const cleanedAllProjects = cleanupComments(allProjectsFound); + if (cleanedAllProjects.length > 0) { + // I found an all projects + const rightRepository = theRightRepository(cleanedRepositories, cleanedAllProjects); - if (cleanedAllProjects.length > 0) { - // ACHEI UM ALL PROJECTS - const rightRepository = theRightRepository(cleanedRepositories, cleanedAllProjects); + if (!Utils.isEmpty(rightRepository)) { + const cursor = rightRepository.text.indexOf(`{`) + 1; + const result = self.findEndBracket(self.mainGradleFilePath(), rightRepository.line, cursor); + return result.line - 1; + } + } else { + throw `didnt find an allprojects`; + } - if (!Utils.isEmpty(rightRepository)) { - const cursor = rightRepository.text.indexOf(`{`) + 1; - // const line = getRepositoryLine(repositoryInfo.url); TODO ISTO NAO DEVIA ESTAR AQUI - const result = self.findEndBracket(self.mainGradleFilePath(), rightRepository.line, cursor); - // ENDINGBRACKET - 1 in position should be the place - return result.line - 1; - } - } else { - throw `didnt find an allprojects`; - } - }); - }); - }); } };