mirror of
https://github.com/ypresto/SwiftLintXcode.git
synced 2026-03-25 08:55:51 +00:00
Set working directory to workspace root directory
This commit is contained in:
parent
4246f4cc72
commit
98c95a2143
5 changed files with 57 additions and 6 deletions
|
|
@ -9,6 +9,7 @@
|
|||
/* Begin PBXBuildFile section */
|
||||
0498717F1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m in Sources */ = {isa = PBXBuildFile; fileRef = 0498717E1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m */; };
|
||||
049871811CB28EBF00C5F7B5 /* Formatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049871801CB28EBF00C5F7B5 /* Formatter.swift */; };
|
||||
04DFAD471CB508D1007998DF /* SwiftLintXcodeIDEHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 04DFAD461CB508D1007998DF /* SwiftLintXcodeIDEHelper.m */; };
|
||||
04DFAD491CB50BC8007998DF /* errorHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04DFAD481CB50BC8007998DF /* errorHelper.swift */; };
|
||||
04E4BF341CB25D3200BC7305 /* SwiftLintXcode.xcscheme in Resources */ = {isa = PBXBuildFile; fileRef = 04E4BF331CB25D3200BC7305 /* SwiftLintXcode.xcscheme */; };
|
||||
04E4BF361CB25D3200BC7305 /* SwiftLintXcode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04E4BF351CB25D3200BC7305 /* SwiftLintXcode.swift */; };
|
||||
|
|
@ -21,6 +22,8 @@
|
|||
0498717D1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftLintXcodeTRVSXcode.h; sourceTree = "<group>"; };
|
||||
0498717E1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SwiftLintXcodeTRVSXcode.m; sourceTree = "<group>"; };
|
||||
049871801CB28EBF00C5F7B5 /* Formatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Formatter.swift; sourceTree = "<group>"; };
|
||||
04DFAD451CB508D1007998DF /* SwiftLintXcodeIDEHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftLintXcodeIDEHelper.h; sourceTree = "<group>"; };
|
||||
04DFAD461CB508D1007998DF /* SwiftLintXcodeIDEHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SwiftLintXcodeIDEHelper.m; sourceTree = "<group>"; };
|
||||
04DFAD481CB50BC8007998DF /* errorHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = errorHelper.swift; sourceTree = "<group>"; };
|
||||
04E4BF2F1CB25D3100BC7305 /* SwiftLintXcode.xcplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftLintXcode.xcplugin; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
04E4BF331CB25D3200BC7305 /* SwiftLintXcode.xcscheme */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = SwiftLintXcode.xcscheme; path = SwiftLintXcode.xcodeproj/xcshareddata/xcschemes/SwiftLintXcode.xcscheme; sourceTree = SOURCE_ROOT; };
|
||||
|
|
@ -52,6 +55,8 @@
|
|||
children = (
|
||||
0498717D1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.h */,
|
||||
0498717E1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m */,
|
||||
04DFAD451CB508D1007998DF /* SwiftLintXcodeIDEHelper.h */,
|
||||
04DFAD461CB508D1007998DF /* SwiftLintXcodeIDEHelper.m */,
|
||||
04E4BF351CB25D3200BC7305 /* SwiftLintXcode.swift */,
|
||||
04E4BF371CB25D3200BC7305 /* NSObject_Extension.swift */,
|
||||
04E4BF401CB273A700BC7305 /* SaveHook.swift */,
|
||||
|
|
@ -142,6 +147,7 @@
|
|||
0498717F1CB27F8900C5F7B5 /* SwiftLintXcodeTRVSXcode.m in Sources */,
|
||||
04E4BF361CB25D3200BC7305 /* SwiftLintXcode.swift in Sources */,
|
||||
049871811CB28EBF00C5F7B5 /* Formatter.swift in Sources */,
|
||||
04DFAD471CB508D1007998DF /* SwiftLintXcodeIDEHelper.m in Sources */,
|
||||
04DFAD491CB50BC8007998DF /* errorHelper.swift in Sources */,
|
||||
04E4BF411CB273A700BC7305 /* SaveHook.swift in Sources */,
|
||||
04E4BF381CB25D3200BC7305 /* NSObject_Extension.swift in Sources */,
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ import Cocoa
|
|||
|
||||
final class Formatter {
|
||||
static var sharedInstance = Formatter()
|
||||
private static let pathExtension = "SwiftLintXcode"
|
||||
|
||||
private static let pathExtension = "SwiftLintXcode"
|
||||
private let fileManager = NSFileManager.defaultManager()
|
||||
|
||||
private struct CursorPosition {
|
||||
|
|
@ -71,12 +71,18 @@ final class Formatter {
|
|||
}
|
||||
|
||||
private func formatString(string: String) throws -> String {
|
||||
guard let workspaceRootDirectory = SwiftLintXcodeIDEHelper.currentWorkspaceURL()?.URLByDeletingLastPathComponent?.path else {
|
||||
throw errorWithMessage("Cannot determine project directory.")
|
||||
}
|
||||
|
||||
return try withTempporaryFile { (filePath) in
|
||||
try string.writeToFile(filePath, atomically: false, encoding: NSUTF8StringEncoding)
|
||||
let swiftlintPath = try self.getExecutableOnPath("swiftlint")
|
||||
let task = NSTask.launchedTaskWithLaunchPath(swiftlintPath, arguments: [
|
||||
"autocorrect", "--path", filePath
|
||||
])
|
||||
let swiftlintPath = try self.getExecutableOnPath(name: "swiftlint", workingDirectory: workspaceRootDirectory)
|
||||
let task = NSTask()
|
||||
task.launchPath = swiftlintPath
|
||||
task.arguments = ["autocorrect", "--path", filePath]
|
||||
task.currentDirectoryPath = workspaceRootDirectory
|
||||
task.launch()
|
||||
task.waitUntilExit()
|
||||
if task.terminationStatus != 0 {
|
||||
throw errorWithMessage("Executing swiftlint exited with non-zero status.")
|
||||
|
|
@ -85,13 +91,14 @@ final class Formatter {
|
|||
}
|
||||
}
|
||||
|
||||
private func getExecutableOnPath(name: String) throws -> String {
|
||||
private func getExecutableOnPath(name name: String, workingDirectory: String) throws -> String {
|
||||
let pipe = NSPipe()
|
||||
let task = NSTask()
|
||||
task.launchPath = "/bin/bash"
|
||||
task.arguments = [
|
||||
"-l", "-c", "which \(name)"
|
||||
]
|
||||
task.currentDirectoryPath = workingDirectory
|
||||
task.standardOutput = pipe
|
||||
task.launch()
|
||||
task.waitUntilExit()
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@
|
|||
//
|
||||
|
||||
#import "SwiftLintXcodeTRVSXcode.h"
|
||||
#import "SwiftLintXcodeIDEHelper.h"
|
||||
|
|
|
|||
15
SwiftLintXcode/SwiftLintXcodeIDEHelper.h
Normal file
15
SwiftLintXcode/SwiftLintXcodeIDEHelper.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
//
|
||||
// SwiftLintXcodeIDEHelper.h
|
||||
// SwiftLintXcode
|
||||
//
|
||||
// Created by yuya.tanaka on 2016/04/06.
|
||||
// Copyright © 2016年 Yuya Tanaka. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface SwiftLintXcodeIDEHelper : NSObject
|
||||
|
||||
+ (nullable NSURL *)currentWorkspaceURL;
|
||||
|
||||
@end
|
||||
22
SwiftLintXcode/SwiftLintXcodeIDEHelper.m
Normal file
22
SwiftLintXcode/SwiftLintXcodeIDEHelper.m
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
//
|
||||
// SwiftLintXcodeIDEHelper.m
|
||||
// SwiftLintXcode
|
||||
//
|
||||
// Created by yuya.tanaka on 2016/04/06.
|
||||
// Copyright © 2016年 Yuya Tanaka. All rights reserved.
|
||||
//
|
||||
|
||||
#import "SwiftLintXcodeIDEHelper.h"
|
||||
#import "SwiftLintXcodeTRVSXcode.h"
|
||||
@import Cocoa;
|
||||
|
||||
@implementation SwiftLintXcodeIDEHelper
|
||||
|
||||
+ (nullable NSURL *)currentWorkspaceURL
|
||||
{
|
||||
IDEWorkspaceWindowController *workspaceWindowController = (IDEWorkspaceWindowController *)[[NSApp keyWindow] windowController];
|
||||
IDEWorkspace *workspace = [workspaceWindowController valueForKey:@"_workspace"];
|
||||
return workspace.representingFilePath.fileURL;
|
||||
}
|
||||
|
||||
@end
|
||||
Loading…
Reference in a new issue