blob: 4efae072113e71f8389429ab0b16fd470c9f4ec5 [file] [log] [blame]
/*
* Copyright 2012-present Facebook, Inc.
*
* Licensed 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 com.facebook.buck.step.fs;
import com.facebook.buck.step.ExecutionContext;
import com.facebook.buck.step.Step;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.file.Path;
public class SymlinkFileStep implements Step {
private final String source;
private final String target;
public SymlinkFileStep(String source, String target) {
this.source = Preconditions.checkNotNull(source);
this.target = Preconditions.checkNotNull(target);
}
@Override
public String getShortName() {
return "symlink_file";
}
@Override
public String getDescription (ExecutionContext context) {
Function<String, Path> pathRelativizer = context.getProjectFilesystem().getPathRelativizer();
// Always symlink to an absolute path so the symlink is sure to be read correctly.
return Joiner.on(" ").join(
"ln",
"-f",
"-s",
pathRelativizer.apply(source),
pathRelativizer.apply(target));
}
@Override
public int execute(ExecutionContext context) {
Function<String, Path> pathRelativizer = context.getProjectFilesystem().getPathRelativizer();
Path targetPath = pathRelativizer.apply(target);
Path sourcePath = pathRelativizer.apply(source);
try {
context.getProjectFilesystem().createSymLink(sourcePath, targetPath, true);
return 0;
} catch (IOException e) {
e.printStackTrace(context.getStdErr());
return 1;
}
}
}