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

How to add source (class & header) with specific path ? #33

Open
iziz opened this issue Feb 12, 2014 · 12 comments
Open

How to add source (class & header) with specific path ? #33

iziz opened this issue Feb 12, 2014 · 12 comments

Comments

@iziz
Copy link

iziz commented Feb 12, 2014

Is this is possible to add source to group with specific path ?

@jasperblues
Copy link
Member

I think not currently, though it should be very simple to support.

@iziz
Copy link
Author

iziz commented Feb 12, 2014

I was edited in XCGroup
But fail in XCTarget addMember (invalid path)
Could you give me some help ?

- (void)addSourceFile:(XCSourceFileDefinition*)sourceFileDefinition withPath:(NSString*)targetPath
{
    [self makeGroupMemberWithName:[sourceFileDefinition sourceFileName] contents:[sourceFileDefinition data]
                             type:[sourceFileDefinition type] fileOperationStyle:[sourceFileDefinition fileOperationStyle]
                         withPath:targetPath];
    [[_project objects] setObject:[self asDictionary] forKey:_key];
}

- (void)makeGroupMemberWithName:(NSString*)name contents:(id)contents type:(XcodeSourceFileType)type
             fileOperationStyle:(XcodeFileOperationStyle)fileOperationStyle
                       withPath:(NSString*)targetPath
{
    NSString* filePath;
    XCSourceFile* currentSourceFile = (XCSourceFile*) [self memberWithDisplayName:name];
    if ((currentSourceFile) == nil)
    {
        NSDictionary* reference = [self makeFileReferenceWithPath:name
                                                             name:nil type:type];
        NSString* fileKey = [[XCKeyBuilder forItemNamed:[NSString stringWithFormat:@"%@/%@", targetPath, name]] build];

        [[_project objects] setObject:reference forKey:fileKey];
        [self addMemberWithKey:fileKey];
    }

    filePath = targetPath;

    BOOL writeFile = NO;
    if (fileOperationStyle == FileOperationStyleOverwrite)
    {
        writeFile = YES;
        [_fileOperationQueue fileWithName:name existsInProjectDirectory:filePath];
    }
    else if (fileOperationStyle == FileOperationStyleAcceptExisting &&
             ![_fileOperationQueue fileWithName:name existsInProjectDirectory:filePath])
    {
        writeFile = YES;
    }

    if (writeFile)
    {
        if ([contents isKindOfClass:[NSString class]])
        {
            [_fileOperationQueue queueTextFile:name inDirectory:filePath withContents:contents];
        }
        else
        {
            [_fileOperationQueue queueDataFile:name inDirectory:filePath withContents:contents];
        }
    }
}

@jasperblues
Copy link
Member

There's a different flag for relative vs absolute file paths.

  • Try creating an absolute file in Xcode. . open the project.pbxproj and take a look.

@iziz
Copy link
Author

iziz commented Feb 12, 2014

Ah, I did not want to add with absolute path. (sorry didn't mentioned that before)
I tried to add with relative path.

Anyways, Thank for your answer.

@jasperblues
Copy link
Member

Did you try running all of the tests? There's a sample project that gets unpacked into /tmp

You can observe the updates there by opening that project in Xcode.

  • Currently you can add a source- file by specifying the contents of the file.
  • To use an existing file on the disk, and adding it into the project is not really supported, unless you put the file in the correct place.
  • You could read the file from disk and set it as the argument to XcodeEditor.

@iziz
Copy link
Author

iziz commented Feb 13, 2014

Yes. I think it need support for

path = IZIZ/TIimaveView.m; 

or

path = ../IZIZ/TIimaveView.m; 

or

path = II/ZZ/TIimaveView.m; 

instead of

path = TIimaveView.m; 

in

FD88D16718ACA2EE00B74C6D /* TIimaveView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TIimaveView.m; path = IZIZ/TIimaveView.m; sourceTree = "<group>";

@jasperblues
Copy link
Member

Its possible to add the source with a specific path, if you create groups as needed as you go. Will this work for you?

@iziz
Copy link
Author

iziz commented May 21, 2014

Sorry for late ACK
I'm using below with edited XCGroup file

NSString *projectName = @"RestInPeace";
NSString *workingRoot = @(getenv("WORKING_SRC_ROOT"));

unsigned long startPosition = workingRoot.length - projectName.length;

NSRange replacementRange = NSMakeRange(startPosition, projectName.length);
NSString *targetProjectFile = [workingRoot stringByReplacingCharactersInRange:replacementRange
                                                                   withString:TARGET_PROJECT];

XCProject *project = [[XCProject alloc] initWithFilePath:targetProjectFile];
XCGroup *currentTargetGroup = [project groupWithPathFromRoot:targetGroup];

XCSourceFileDefinition *newSourceFile =
    [[XCSourceFileDefinition alloc] initWithName:fileName text:content type:sourceType];

[newSourceFile setFileOperationType:XCFileOperationTypeOverwrite];
[currentTargetGroup addSourceFile:newSourceFile withPath:TARGET_NAME];

if (shouldAddToTarget) {
    XCSourceFile *sourceFile = [project fileWithName:fileName];
    XCTarget *targetObj = [project targetWithName:TARGET_NAME];
    [targetObj addMember:sourceFile];

    XCTarget *targetObjDev = [project targetWithName:TARGET_NAME_DEV];
    [targetObjDev addMember:sourceFile];
}

XCGroup.h

- (void)addSourceFile:(XCSourceFileDefinition*)sourceFileDefinition withPath:(NSString*)pathPrefix;

XCGroup.m

- (void)makeGroupMemberWithName:(NSString*)name contents:(id)contents type:(XcodeSourceFileType)type
             fileOperationStyle:(XCFileOperationType)fileOperationStyle withPath:(NSString*)targetPath
...
- (void)addSourceFile:(XCSourceFileDefinition*)sourceFileDefinition withPath:(NSString*)pathPrefix
{
    [self makeGroupMemberWithName:[sourceFileDefinition sourceFileName] contents:[sourceFileDefinition data]
                             type:[sourceFileDefinition type] fileOperationStyle:[sourceFileDefinition fileOperationType]
                         withPath:targetPath];
    ....
}

@iziz iziz closed this as completed May 21, 2014
@jasperblues
Copy link
Member

You mean its working OK for you now?

What is the edit? Something you went merged into the main XcodeEditor branch?

@iziz
Copy link
Author

iziz commented May 21, 2014

Yep! OK
I will create merge request~

@jasperblues jasperblues reopened this May 21, 2014
@jasperblues
Copy link
Member

Ok, glad to hear it's working for you now.

@abhisheksaatal
Copy link

abhisheksaatal commented Jul 1, 2016

Hi iziz/jasper,

I am not able to find this api with the parameter withPath,
is it available in some other branch?

[group addSourceFile:newSourceFile withPath:TARGET_NAME];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants