Skip to content

convert EmptyDirTask.groovy to .java #34672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

83 changes: 83 additions & 0 deletions buildSrc/src/main/java/org/elasticsearch/gradle/EmptyDirTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.gradle;

import java.io.File;

import javax.inject.Inject;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.nativeintegration.filesystem.Chmod;

/**
* Creates an empty directory.
*/
public class EmptyDirTask extends DefaultTask {

private File dir;
private int dirMode = 0755;

/**
* Creates an empty directory with the configured permissions.
*/
@TaskAction
public void create() {
dir.mkdirs();
getChmod().chmod(dir, dirMode);
}

@Inject
public Chmod getChmod() {
throw new UnsupportedOperationException();
}

@Input
public File getDir() {
return dir;
}

/**
* @param dir The directory to create
*/
public void setDir(File dir) {
this.dir = dir;
}

/**
* @param dir The path of the directory to create. Takes a String and coerces it to a file.
*/
public void setDir(String dir) {
this.dir = getProject().file(dir);
}

@Input
public int getDirMode() {
return dirMode;
}

/**
* @param dirMode The permissions to apply to the new directory
*/
public void setDirMode(int dirMode) {
this.dirMode = dirMode;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.gradle;

import java.io.File;
import java.io.IOException;

import org.elasticsearch.gradle.test.GradleUnitTestCase;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;

public class EmptyDirTaskTests extends GradleUnitTestCase {

public void testCreateEmptyDir() throws Exception {
Project project = createProject();
EmptyDirTask emptyDirTask = createTask(project);
assertEquals(0755, emptyDirTask.getDirMode());

// generate a new temporary folder and make sure it does not exists
File newEmptyFolder = getNewNonExistingTempFolderFile(project);

emptyDirTask.setDir(newEmptyFolder);
emptyDirTask.create();

assertTrue(newEmptyFolder.exists());
assertTrue(newEmptyFolder.isDirectory());
assertTrue(newEmptyFolder.canExecute());
assertTrue(newEmptyFolder.canRead());
assertTrue(newEmptyFolder.canWrite());

// cleanup
newEmptyFolder.delete();
}

public void testCreateEmptyDirNoPermissions() throws Exception {
Project project = createProject();
EmptyDirTask emptyDirTask = createTask(project);
emptyDirTask.setDirMode(0000);

// generate a new temporary folder and make sure it does not exists
File newEmptyFolder = getNewNonExistingTempFolderFile(project);

emptyDirTask.setDir(newEmptyFolder);
emptyDirTask.create();

assertTrue(newEmptyFolder.exists());
assertTrue(newEmptyFolder.isDirectory());
assertFalse(newEmptyFolder.canExecute());
assertFalse(newEmptyFolder.canRead());
assertFalse(newEmptyFolder.canWrite());

// cleanup
newEmptyFolder.delete();
}

private File getNewNonExistingTempFolderFile(Project project) throws IOException {
File newEmptyFolder = new File(project.getBuildDir(), "empty-dir");
assertFalse(newEmptyFolder.exists());
return newEmptyFolder;
}

private Project createProject() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please inline this and the next method as they are now just one-liners.

return ProjectBuilder.builder().build();
}

private EmptyDirTask createTask(Project project) {
return project.getTasks().create("emptyDirTask", EmptyDirTask.class);
}
}
8 changes: 4 additions & 4 deletions distribution/archives/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ apply plugin: 'base'
// parent to copy to the root of the distribution
ext.logsDir = new File(buildDir, 'logs-hack/logs')
task createLogsDir(type: EmptyDirTask) {
dir "${logsDir}"
dirMode 0755
dir = "${logsDir}"
dirMode = 0755
}
ext.pluginsDir= new File(buildDir, 'plugins-hack/plugins')
task createPluginsDir(type: EmptyDirTask) {
dir "${pluginsDir}"
dirMode 0755
dir = "${pluginsDir}"
dirMode = 0755
}

CopySpec archiveFiles(CopySpec modulesFiles, String distributionType, String platform, boolean oss) {
Expand Down