11package io .github .fvarrui .javapackager .gradle ;
22
33import java .io .File ;
4- import java .io .FileFilter ;
54import java .io .IOException ;
65import java .security .NoSuchAlgorithmException ;
6+ import java .util .ArrayList ;
7+ import java .util .List ;
78
89import org .redline_rpm .Builder ;
910import org .redline_rpm .header .Architecture ;
1011import org .redline_rpm .header .Os ;
1112import org .redline_rpm .header .RpmType ;
12- import org .redline_rpm .payload .Directive ;
1313
1414import io .github .fvarrui .javapackager .packagers .ArtifactGenerator ;
1515import io .github .fvarrui .javapackager .packagers .LinuxPackager ;
1616import io .github .fvarrui .javapackager .packagers .Packager ;
1717import io .github .fvarrui .javapackager .utils .FileUtils ;
1818import io .github .fvarrui .javapackager .utils .Logger ;
19+ import io .github .fvarrui .javapackager .utils .VelocityUtils ;
1920
2021/**
2122 * Creates a RPM package file including all app folder's content only for
@@ -43,6 +44,17 @@ protected File doApply(Packager packager) throws Exception {
4344 String description = linuxPackager .getDescription ();
4445 String organizationName = linuxPackager .getOrganizationName ();
4546 File outputDirectory = linuxPackager .getOutputDirectory ();
47+ File executable = linuxPackager .getExecutable ();
48+ File assetsFolder = linuxPackager .getAssetsFolder ();
49+ String jreDirectoryName = linuxPackager .getJreDirectoryName ();
50+
51+ // generates desktop file from velocity template
52+ File desktopFile = new File (assetsFolder , name + ".desktop" );
53+ VelocityUtils .render ("linux/desktop.vtl" , desktopFile , linuxPackager );
54+ Logger .info ("Rendering desktop file to " + desktopFile .getAbsolutePath ());
55+
56+ // copies desktop file to app
57+ FileUtils .copyFileToFolder (desktopFile , appFolder );
4658
4759 Builder builder = new Builder ();
4860 builder .setType (RpmType .BINARY );
@@ -51,29 +63,44 @@ protected File doApply(Packager packager) throws Exception {
5163 builder .setPackager (organizationName );
5264 builder .setDescription (description );
5365 builder .setPrefixes ("opt" );
54-
55- // TODO add directories tree and all app files
56- addDirectoryTree (builder , "" , appFolder );
5766
58- // builder.addFile("HelloWorldMaven/HelloWorldMaven", new File(appFolder,
59- // "HelloWorldMaven"), 0755);
67+ // list of files which needs execution permissions
68+ List <File > executionPermissions = new ArrayList <>();
69+ executionPermissions .add (executable );
70+ executionPermissions .add (new File (appFolder , jreDirectoryName + "/bin/java" ));
71+ executionPermissions .add (new File (appFolder , jreDirectoryName + "/lib/jspawnhelper" ));
72+
73+ // add all app files
74+ addDirectoryTree (builder , "/opt" , appFolder , executionPermissions );
75+
76+ // link to desktop file
77+ builder .addLink ("/usr/share/applications/" + desktopFile .getName (), "/opt/" + name + "/" + desktopFile .getName ());
78+
79+ // link to binary
80+ builder .addLink ("/usr/local/bin/" + executable .getName (), "/opt/" + name + "/" + executable .getName ());
6081
6182 builder .build (outputDirectory );
6283
63- File rpm = new File (outputDirectory , name + "-" + version + "-1.x86_64.rpm" );
64- if (rpm .exists ()) {
65- File rpmOutput = new File (outputDirectory , name + "_" + version + ".rpm" );
66- FileUtils .rename (rpm , rpmOutput .getName ());
67- return rpmOutput ;
84+ File originalRpm = new File (outputDirectory , name + "-" + version + "-1.x86_64.rpm" );
85+ File rpm = null ;
86+ if (originalRpm .exists ()) {
87+ rpm = new File (outputDirectory , name + "_" + version + ".rpm" );
88+ if (rpm .exists ()) rpm .delete ();
89+ FileUtils .rename (originalRpm , rpm .getName ());
6890 }
6991
70- return null ;
92+ return rpm ;
7193 }
7294
73- private void addDirectoryTree (Builder builder , String parentPath , File root ) throws NoSuchAlgorithmException , IOException {
74- builder .addDirectory (parentPath + "/" + root .getName ());
75- for (File dir : root .listFiles (f -> f .isDirectory ())) {
76- addDirectoryTree (builder , parentPath + "/" + root .getName (), dir );
95+ private void addDirectoryTree (Builder builder , String parentPath , File root , List <File > executionPermissions ) throws NoSuchAlgorithmException , IOException {
96+ String rootPath = parentPath + "/" + root .getName ();
97+ builder .addDirectory (rootPath );
98+ for (File f : root .listFiles ()) {
99+ if (f .isDirectory ())
100+ addDirectoryTree (builder , parentPath + "/" + root .getName (), f , executionPermissions );
101+ else {
102+ builder .addFile (rootPath + "/" + f .getName (), f , executionPermissions .contains (f ) ? 0755 : 0644 );
103+ }
77104 }
78105 }
79106
0 commit comments