Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarferreira committed Nov 7, 2017
1 parent 8ecd632 commit b2f9e34
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 96 deletions.
59 changes: 31 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <library>` 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 `<library>` `<module>`
--------

Single library
> drone add `picasso` app
Multiple
> drone add `picasso` `retrofit` `rxjava` `gson` app
<p align="center">
<img src="extras/add3.gif" width="100%" />
<img src="extras/add4.gif" width="100%" />
</p>

```
Expand Down Expand Up @@ -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 <library>` 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)!
Expand Down
Binary file added extras/add4.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 45 additions & 38 deletions src/tasks/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}

Expand All @@ -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);
Expand All @@ -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) => {
Expand All @@ -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()
}
};
9 changes: 5 additions & 4 deletions src/tasks/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};
45 changes: 19 additions & 26 deletions src/utils/gradle_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down Expand Up @@ -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`;
}
});
});
});
}
};

0 comments on commit b2f9e34

Please sign in to comment.