Skip to content
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

Add yaml-request command #315

Merged
merged 2 commits into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

##### Enhancements

* Add `Request.yaml` to create a sourcekit request from yaml.
* Add `Request.yaml` API to create a sourcekit request from yaml
and expose as a `request --yaml [file|text]` CLI command.
[Keith Smiley](https://github.com/keith)
[#313](https://github.com/jpsim/SourceKitten/pull/313)
[#312](https://github.com/jpsim/SourceKitten/issues/312)

##### Bug Fixes

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ Running `sourcekitten syntax --file file.swift` or `sourcekitten syntax --text "
]
```

## Request

Running `sourcekitten request --yaml [FILE|TEXT]` will execute a sourcekit request with the given yaml. For example:

```yaml
key.request: source.request.cursorinfo
key.sourcefile: "/tmp/foo.swift"
key.offset: 8
key.compilerargs:
- "/tmp/foo.swift"
```

## SourceKittenFramework

Most of the functionality of the `sourcekitten` command line tool is actually encapsulated in a framework named SourceKittenFramework.
Expand Down
47 changes: 47 additions & 0 deletions Source/sourcekitten/YamlRequestCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// YamlCommand.swift
// SourceKitten
//
// Created by Keith Smiley on 12/12/16.
// Copyright © 2016 SourceKitten. All rights reserved.
//

import Commandant
import Foundation
import Result
import SourceKittenFramework

struct RequestCommand: CommandProtocol {
let verb = "request"
let function = "Run a raw sourcekit request"

struct Options: OptionsProtocol {
let yaml: String

static func create(yaml: String) -> Options {
return self.init(yaml: yaml)
}

static func evaluate(_ m: CommandMode) -> Result<Options, CommandantError<SourceKittenError>> {
return create
<*> m <| Option(key: "yaml", defaultValue: "", usage: "a path to a yaml file, or yaml text to execute")
}
}

func run(_ options: Options) -> Result<(), SourceKittenError> {
if options.yaml.isEmpty {
return .failure(.invalidArgument(description: "yaml file or text must be provided"))
}

let yaml: String
if let file = File(path: options.yaml) {
yaml = file.contents
} else {
yaml = options.yaml
}

let request = Request.yamlRequest(yaml: yaml)
print(toJSON(toNSDictionary(request.send())))
return .success()
}
}
1 change: 1 addition & 0 deletions Source/sourcekitten/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ DispatchQueue.global(qos: .default).async {
registry.register(IndexCommand())
registry.register(SyntaxCommand())
registry.register(StructureCommand())
registry.register(RequestCommand())
registry.register(VersionCommand())
registry.register(HelpCommand(registry: registry))

Expand Down
4 changes: 4 additions & 0 deletions sourcekitten.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
6CCFCE8E1CFED000003239EB /* Commandant.framework in Embed Frameworks into SourceKittenFramework.framework */ = {isa = PBXBuildFile; fileRef = E8EBAA5D1A5D374B002F1B8E /* Commandant.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
6CCFCE901CFED005003239EB /* Result.framework in Embed Frameworks into SourceKittenFramework.framework */ = {isa = PBXBuildFile; fileRef = E834D61D1B2D054B002AA1FE /* Result.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
B2FA87E26BC9C5F774FD694C /* SwiftLangSyntax.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2FA804AA9D4427FF571EFB2 /* SwiftLangSyntax.swift */; };
C236E84B1DFF5120003807D2 /* YamlRequestCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = C236E84A1DFF5120003807D2 /* YamlRequestCommand.swift */; };
D0AAAB5019FB0960007B24B3 /* SourceKittenFramework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SourceKittenFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
D0D1217219E87B05005E4BAA /* SourceKittenFramework.h in Headers */ = {isa = PBXBuildFile; fileRef = D0D1217119E87B05005E4BAA /* SourceKittenFramework.h */; settings = {ATTRIBUTES = (Public, ); }; };
D0D1217819E87B05005E4BAA /* SourceKittenFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SourceKittenFramework.framework */; };
Expand Down Expand Up @@ -141,6 +142,7 @@
6C4CF6491C79802A008532C5 /* library_wrapper_Documentation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = library_wrapper_Documentation.swift; sourceTree = "<group>"; };
6CFC18F01C7F2FB900CD70E1 /* module.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
B2FA804AA9D4427FF571EFB2 /* SwiftLangSyntax.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftLangSyntax.swift; sourceTree = "<group>"; };
C236E84A1DFF5120003807D2 /* YamlRequestCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YamlRequestCommand.swift; sourceTree = "<group>"; };
D0D1211B19E87861005E4BAA /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; usesTabs = 0; };
D0D1212419E878CC005E4BAA /* Common.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Common.xcconfig; sourceTree = "<group>"; };
D0D1212619E878CC005E4BAA /* Debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -301,6 +303,7 @@
D0D1211B19E87861005E4BAA /* main.swift */,
E80604B21A5D452C0016D959 /* StructureCommand.swift */,
E83A0B361A5D399A0041A60A /* SyntaxCommand.swift */,
C236E84A1DFF5120003807D2 /* YamlRequestCommand.swift */,
E83A0B341A5D382B0041A60A /* VersionCommand.swift */,
);
name = sourcekitten;
Expand Down Expand Up @@ -716,6 +719,7 @@
E813023B1CCD09DB0056E826 /* IndexCommand.swift in Sources */,
E8DD06E61AE44540006D9C86 /* StructureCommand.swift in Sources */,
E8DD06E71AE447AB006D9C86 /* SyntaxCommand.swift in Sources */,
C236E84B1DFF5120003807D2 /* YamlRequestCommand.swift in Sources */,
E83A0B351A5D382B0041A60A /* VersionCommand.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down