0

Hi there I've just started using & exploring Shell Scripts so please be patient, alright :)

What I'm trying to accomplish: To display the current iOS projects Total lines of code.

What I don't know: How to take this information and use it in my iOS project and to finally display the total lines of code in a UILabel. (eg. label.text = @"totallinesofcode";)

How do I do this? Cheers, Daniel Ran

What I've got so far: I've been able to attract the total lines of code information by passing these lines...

cd Desktop/Project\ Connection/Connection 
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.c -o -iname \*.cc -o -iname \*.h \) -exec wc -l '{}' \+
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Why would you display it in an iOS app? Why not in the console during the build or on demand? – Wain Jan 18 '14 at 13:37
  • Because I'm adding a feature only assessable for "VIP's" in the application, which has stored a ton of info about the app like, number of active users, total lines of code etc.. Thanks for replying :) – Daniel Ran Lehmann Jan 18 '14 at 13:39

2 Answers2

1

Well, if you know the total lines of code, then just create and int and then :

In your .h :

int *totalLines;

In your .m :

totalLines = xxx(your total lines of code);
label.text = [NSString stringWithFormat:@"Number of Lines: %i, totalLines];

or even more easy just do :

label.text = [NSString stringWithFormat @"Number of Lines: 3289923]; 

(of course that number should be your real number of lines)

Then, if you update the app and add more lines of code, just update your integer or label. I think you are overcomplicating a simple thing.

Mykod
  • 587
  • 1
  • 5
  • 16
  • awesome answer, of course this is one way of doing this, so I'm going to mark it as the right answer :) – Daniel Ran Lehmann Jan 18 '14 at 15:41
  • 1
    Aside: you can add your script to the Xcode build and have it copy the determined count to the info.plist so you can access it at runtime. – Wain Jan 18 '14 at 15:48
0

The above answers lack the bash part. I'm suggesting to create a header file containing the number of lines. Then your code can #import that info and put it into a label as described by others.

The bash part should look like this:

cnt=`find . \( -iname \*.m -o -iname \*.mm -o -iname \*.c -o -iname \*.cc -o -iname \*.h \) -exec cat '{}' \+ | wc -l`
echo "#define NUM_LINES $cnt" > lines_count.h

Your original find command would yield multiple lines, one per file. Instead you should cat everything to the output, and direct it into wc -l

after running these two lines, you can:

#import "lines_count.h"
...
self.myLabel.text = [NSString stringWithFormat:@"lines=%i", NUM_LINES];
ishahak
  • 6,585
  • 5
  • 38
  • 56
  • I'm setting this as the right answer as that's what I was looking for exactly :) cheers! - dan – Daniel Ran Lehmann Jan 19 '14 at 22:13
  • hi again @ishahak just tried it out on my own, but ran into some issues... How do you add the bash command to the header file? I created one and it looks like this: https://scontent-a-lhr.xx.fbcdn.net/hphotos-ash3/t1/72856_10203017634511800_723205011_n.jpg – Daniel Ran Lehmann Jan 19 '14 at 23:06
  • I cannot see your image. In order to upload an image, press the image icon in the editor. However I hope you understand that the bash command should be put into a separate .sh file. You can automate it by adding it to the build phases of xcode. See http://stackoverflow.com/questions/976454/xcode-running-a-script-before-every-build-that-modifies-source-code-directly – ishahak Jan 20 '14 at 02:34