Skip to content

Commit

Permalink
feat: place import command line tool
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshengzhi committed Jun 22, 2024
1 parent 3b55bdd commit e698d43
Show file tree
Hide file tree
Showing 62 changed files with 985 additions and 247 deletions.
172 changes: 161 additions & 11 deletions SZEditorExtension.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "245E06A72C26700600F41C75"
BuildableName = "SZPlaceImports"
BlueprintName = "SZPlaceImports"
ReferencedContainer = "container:SZEditorExtension.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES"
viewDebuggingEnabled = "No">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "245E06A72C26700600F41C75"
BuildableName = "SZPlaceImports"
BlueprintName = "SZPlaceImports"
ReferencedContainer = "container:SZEditorExtension.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "245E06A72C26700600F41C75"
BuildableName = "SZPlaceImports"
BlueprintName = "SZPlaceImports"
ReferencedContainer = "container:SZEditorExtension.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -53,14 +51,15 @@
ReferencedContainer = "container:SZEditorExtension.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = ""
selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
launchStyle = "0"
askForAppToLaunch = "Yes"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
Expand All @@ -81,15 +80,14 @@
ReferencedContainer = "container:SZEditorExtension.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES"
askForAppToLaunch = "Yes"
launchAutomaticallySubstyle = "2">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>SZPlaceImports.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>SZXcodeEditorExtension.xcscheme</key>
<dict>
<key>orderHint</key>
Expand All @@ -22,6 +27,11 @@
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>245E06A72C26700600F41C75</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>24A6CD621D96831100ECEF89</key>
<dict>
<key>primary</key>
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2016/9/24.
// Copyright © 2016年 陈圣治. All rights reserved.
//

#import <Cocoa/Cocoa.h>
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2016/9/24.
// Copyright © 2016年 陈圣治. All rights reserved.
//

#import "AppDelegate.h"
Expand Down
2 changes: 1 addition & 1 deletion SZEditorExtension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2016年 陈圣治. All rights reserved.</string>
<string>Copyright © 2016年 Neil. All rights reserved.</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/SZEditorExtensionHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2018/6/10.
// Copyright © 2018 陈圣治. All rights reserved.
//

#ifndef SZEditorExtensionHeader_h
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/SZPropertyGetterModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2018/6/9.
// Copyright © 2018 陈圣治. All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/SZPropertyGetterModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2018/6/9.
// Copyright © 2018 陈圣治. All rights reserved.
//

#import "SZPropertyGetterModel.h"
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/SZPropertyGetterViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2018/6/9.
// Copyright © 2018 陈圣治. All rights reserved.
//

#import <Cocoa/Cocoa.h>
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/SZPropertyGetterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2018/6/9.
// Copyright © 2018 陈圣治. All rights reserved.
//

#import "SZPropertyGetterViewController.h"
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2016/9/24.
// Copyright © 2016年 陈圣治. All rights reserved.
//

#import <Cocoa/Cocoa.h>
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2016/9/24.
// Copyright © 2016年 陈圣治. All rights reserved.
//

#import "ViewController.h"
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtension/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtension
//
// Created by csz on 2016/9/24.
// Copyright © 2016年 陈圣治. All rights reserved.
//

#import <Cocoa/Cocoa.h>
Expand Down
3 changes: 1 addition & 2 deletions SZEditorExtensionTests/SZEditorExtensionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtensionTests
//
// Created by csz on 2016/9/24.
// Copyright © 2016年 陈圣治. All rights reserved.
//

#import <XCTest/XCTest.h>
Expand Down Expand Up @@ -213,7 +212,7 @@ - (void)testSortString {
@"// SZEditorExtension",
@"//",
@"// Created by csz on 2018/6/9.",
@"// Copyright © 2018 陈圣治. All rights reserved.",
@"// Copyright © 2018 Neil. All rights reserved.",
@"//",
@"",
@"#import \"SZPropertyGetterViewController.h\"",
Expand Down
1 change: 0 additions & 1 deletion SZEditorExtensionUITests/SZEditorExtensionUITests.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SZEditorExtensionUITests
//
// Created by csz on 2016/9/24.
// Copyright © 2016年 陈圣治. All rights reserved.
//

#import <XCTest/XCTest.h>
Expand Down
96 changes: 96 additions & 0 deletions SZPlaceImports/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// main.m
// SZPlaceImports
//
// Created by csz on 2024.06.22.
//

#import <Foundation/Foundation.h>
#import "NSArray+SZAddition.h"
#import "NSString+SZAddition.h"
#import "SZPlaceImportHelper.h"

NSArray<NSString *> *splitedLines(NSString *contents) {
NSUInteger start = 0;
NSUInteger count = contents.length;
NSMutableArray *tempArray = [NSMutableArray array];
for (NSUInteger idx = 0; idx < count; idx++) {
unichar c = [contents characterAtIndex:idx];
if (c == '\n') {
NSRange range = NSMakeRange(start, idx + 1 - start);
NSString *line = [contents substringWithRange:range];
[tempArray addObject:line];
start = idx + 1;
}
}
if (start < count) {
NSRange range = NSMakeRange(start, count - start);
NSString *line = [contents substringWithRange:range];
[tempArray addObject:line];
}
return [tempArray copy];
}

BOOL isSupportedFile(NSString *filePath) {
if (![filePath sz_isFileExist]) {
return NO;
}
NSString *extension = filePath.pathExtension.lowercaseString;
if (![extension isEqualToString:@"h"] && ![extension isEqualToString:@"m"]) {
return NO;
}
return YES;
}

void processFile(NSString *filePath) {
@autoreleasepool {
if (!isSupportedFile(filePath)) {
return;
}
NSStringEncoding encoding = NSUTF8StringEncoding;
NSString *contents = [NSString stringWithContentsOfFile:filePath usedEncoding:&encoding error:nil];
if (!contents.length) {
return;
}
NSArray<NSString *> *lines = splitedLines(contents);
lines = [SZPlaceImportHelper processLines:lines
selections:@[]
commandIdentifier:nil];
contents = [lines componentsJoinedByString:@""];
[contents writeToFile:filePath atomically:YES encoding:encoding error:nil];
}
}

NSArray<NSString *> *findSupportedFiles(NSString *directory) {
NSURL *directoryURL = [NSURL fileURLWithPath:directory];
NSDirectoryEnumerator *enumerator = [NSFileManager.defaultManager enumeratorAtURL:directoryURL
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:nil];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSURL *url in enumerator) {
if (!isSupportedFile(url.path)) {
continue;
}
[tempArray addObject:url.path];
}
return [tempArray copy];
}

int main(int argc, const char * argv[]) {
@autoreleasepool {
for (int i = 1; i < argc; i++) {
NSString *path = [[NSString alloc] initWithCString:argv[i] encoding:NSUTF8StringEncoding];
path = [path stringByResolvingSymlinksInPath];
if ([path sz_isDirectory]) {
NSArray *files = findSupportedFiles(path);
for (NSString *file in files) {
processFile(file);
}
} else {
processFile(path);
}
}
}
return 0;
}
2 changes: 1 addition & 1 deletion SZXcodeEditorExtension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
<string>com.apple.dt.Xcode.extension.source-editor</string>
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2016年 陈圣治. All rights reserved.</string>
<string>Copyright © 2016年 Neil. All rights reserved.</string>
</dict>
</plist>
Loading

0 comments on commit e698d43

Please sign in to comment.