How to copy file and rename depending on subfolder - gradle

I am trying to copy a file in gradle and rename it at the same time, where the new name of the file should be dependent of the name of the subfolder it was in.
My file structure looks like this for example:
├── apk
│   └── app-envTest-normalBuild-release.apk
├── logs
│   └── manifest-merger-envTest-normalBuild-release-report.txt
└── mapping
└── envTestNormalBuild
└── release
├── dump.txt
├── mapping.txt
├── seeds.txt
└── usage.txt
I'd like to copy the mapping.txt and rename it. The new name should be mapping-envTestNormalBuild.txt - i.e. should include the name of the subdirectory it was released in.
Can someone tell me if/how this is possible using the gradle copy+rename feature?
I have seen that the reverse is possible, i.e. copying a file like foo-bar.txt to a directory/path foo/bar.txt and similar.

You can try e.g.:
task clean {
doLast {
project.file('dest').deleteDir()
}
}
task cp(type: Copy) {
dependsOn clean
from 'mapping/envTestNormalBuild/release'
into 'dest'
include 'mapping.txt'
eachFile { fcp ->
fcp.name = "$fcp.file.parentFile.parentFile.name-$fcp.name"
}
}
A demo can be found here.

Related

Make gradle point to subdirectory and treat it as a rootProject

I encountered a problem with gradle project structure. I have a task that needs to be realized and some tests are meant to be executed to check whether my project structure is correct and the tasks in gradle execute correctly. However I think I misunderstood instruction a bit and I'm wondering whether I can do something with my current folders structure or If I will have to rewrite the whole project. My current project structure looks like this:
main-repo-folder/
├── docker-related-file
├── rootProject
│ ├── sub-project-1
│ ├── build(output from tasks is created here)
│ ├── build.gradle
│ ├── sub-project-2
│ ├── gradle
│ ├── gradlew
│ ├── gradlew.bat
│ ├── settings.gradle
│ └── src
As you can see, the root project is a directory inside a repo. In order for my tests to execute I think the repo itself must be a root folder (or act as one) because the tests seem to be trying executing there. And here is my question, is it possible to add f.e settings.gradle file in main-repo-folder (at the same level as rootProject folder) to "point" gradle to build from rootProject and treat that folder as the root?(I mean f.e if I call gradle clean build task_name in main-repo-folder I want to make gradle execute it as I would be in rootProject folder)
I've tried to find some information but I'm at the path of learning gradle and I don't know if it is even possible :/ .
Rename main-repo-folder/rootProject to main-repo-folder.

Bash: automatically add a file to a Xcode project?

I am creating a script.sh file that creates a Test.swift file and adds it into a Xcode project. However, I would like to know if there is a way to add this file to Xcode (in the project.pbxproj file) from this script? Instead of doing it manually in Xcode (Add files to Project...).
Thank you
3/05 Update
I tried #Johnykutty answer, here is my current Xcode project before executing the ruby script:
I have already generated a A folder with a Sample.swift file located in test, but these files are not linked to my Xcode project yet:
Now here is the script that I'm executing:
require 'xcodeproj'
project_path = '../TestCodeProjTest.xcodeproj'
project = Xcodeproj::Project.open(project_path)
file_group = project["TestCodeProjTest"]["test"]
file_group.new_file("#{project.project_dir}/TestCodeProjTest/test/A")
project.save()
This almost works fine, except that it creates a folder reference instead of a group, and it doesn't link it to my target:
Hence the content of Sample.swift is unreachable.
Its hard to achieve by bash. But really easy if you use Ruby and xcodeproj gem from Cocoapods
Consider you have file structure like
├── GeneratedFiles
│   └── Sample1.swift
├── MyProject
│   ├── AppDelegate.swift
│   ├── ... all other files
│   ├── SceneDelegate.swift
│   └── ViewController.swift
├── MyProject.xcodeproj
│   ├── project.pbxproj
│   ├── .....
└── add_file.rb
Then you can add files like
require 'xcodeproj'
project_path = 'MyProject.xcodeproj'
project = Xcodeproj::Project.open(project_path)
file_group = project["MyProject"]
file_group.new_file("../GeneratedFiles/Sample1.swift")
project.save()
UPDATE:
project["MyProject"] returns a file group which is a group named MyProject in the root of the project, you can select another group inside MyProject by file_group = project["MyProject"]["MyGroup"]
Then the generated file path should be either related to that group like file_group.new_file("../../GeneratedFiles/Sample1.swift") or full path like file_group.new_file("#{project.project_dir}/GeneratedFiles/Sample1.swift")
More details about Xcodeproj here

Go build doesn't build custom libs

my working tree is like this:
/opt/go/src/tb-to-composer/
├── apis
│   └── rtb.go
├── config.yaml
├── jsondef
│   └── structures.go
├── LICENSE.md
├── README.md
├── tb-to-composer
└── thingsToComposer.go
when I do go build inside /opt/go/src/tb-to-composer/ the build doesn't recompile rtb.go and structures.go even though there was changes in them. In order to achieve build I need to run go build -a every time I do a change to rtb.go or structures.go, is that the expected behavior from go build? How to I recompile only custom libs inside my package folder without recompile the whole /opt/go/src tree?
You can try the -i flag, or (this does not work, sorry) specify the files in the directories explicitly as arguments to go build, i.e. go build thingsToComposer.go apis/rtb.go jsondef/structures.go

Delete subdirectory excluding files in gradle

I have a directory, lets say DirA. There is a subdirectory called DirB and some files in DirA. Now, when I use the Gradle task to delete only the subdirectory (DirB), it is deleting the files also. I don't want it to delete the files. Can anyone please help me achieve this?
task deleteFolder(type: Delete){
delete "full path of subdirectory relative to build.gradle location"
}
With the following structure:
.
└── dira
├── a
├── b
├── c
└── dirb
and script:
task clearDirA(type: Delete) {
delete project.file('dira/dirb')
}

How to set a flattened test/source folder structure?

Using gradle for a Java+Groovy+JUnit project, I get this source folder structure by default:
prjroot-src
├── main
│ ├── groovy
│ └── java
└── test
├── groovy
└── java
I wanted to have Java and Groovy sources in one folder, so I set the sourceSets like this:
sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDirs += ["src/main/java"]
Which resulted in the same directory structure with the difference that I could put my .groovy files in the java folder. Also, the unused Groovy folder still is there.
I'd like a much flatter, cleaner directory structure that looks like this:
prjroot
├── src
└── test
The packages, .groovy and .java files should be directly under src, and the test sources accordingly under test.
How can I achieve this using gradle?
Replace your sourceSets code with:
sourceSets.main.groovy.srcDirs = ["src"]
sourceSets.test.groovy.srcDirs = ["test"]

Resources