Skip to main content

(CI) Jenkins PMD report for Objective-C with OCLint

OCLint is a static code analysis tool for improving quality and reducing defects by inspecting C, C++ and Objective-C code and looking for potential problems like:
  • Possible bugs - empty if/else/try/catch/finally statements
  • Unused code - unused local variables and parameters
  • Complicated code - high cyclomatic complexity, NPath complexity and high NCSS
  • Redundant code - redundant if statement and useless parentheses
  • Code smells - long method and long parameter list
  • Bad practices - inverted logic and parameter reassignment
  • ...

Installation:

  • Step 1: Download "oclint-0.x.x-x86_64-darwin-15.0.0.tar" from the downloads page. Unzip this file and see as below:

  • Step 2: Copying OCLint to System PATH. Open Terminal prompt, and input some commands lines as follows:
  • $ cd /Downloads/oclint-0.10.x
    $ cp bin/oclint* /usr/local/bin/
    $ cp -rp lib/* /usr/local/lib/
    Other option: Directly adding to PATH at here.
  • Checking installation: Open a new terminal prompt, and execute oclint directly from there and expect message similar to below:
  • $ oclint
    oclint: Not enough positional command line arguments specified!
    Must specify at least 1 positional arguments: See: oclint -help

Using OCLint with xcodebuild:

Suppose we have a project with the name as iOSDesignPatterns:

- Create a build script, as follows:
 #!/bin/sh
# Generate PMD reporter
# Step 1: Create xcodebuild.log
xcodebuild clean -project iOSDesignPatterns.xcodeproj/ -scheme 'iOSDesignPatterns' -configuration Debug
xcodebuild build -target iOSDesignPatterns -configuration Debug -scheme iOSDesignPatterns CLANG_ENABLE_MODULE_DEBUGGING=NO CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO | tee xcodebuild.log
# Step 2: Create compile_commands.json
oclint-xcodebuild

# Step 3: Create pmd_oclint.xml
oclint-json-compilation-database -- -report-type pmd -o pmd_oclint.xml

...and run it with:
$ sh build_pmd.sh

After run it successfully, as below:


Using OCLint with Jenkins:

Create a project on Jenkins and clone that project:
Configuring OCLint and PMD plugin:
- Open Configure page of a project on Jenkins CI.
- For execute a script:: Add a new build step and choose "Execute shell". Set up the command as above ("sh build_pmd.sh"). 
- For export PMD report: Add a new post-build action and choose "Publish PMD analysis result". Enter the output artifact name which is defined in the script ("pmd_oclint.xml").
- The result as below:
Build Jenkins CI: You will get an error as below:
If you want to build successful on Jenkins CI, you will need to add some arguments of oclint-json-compilation-database, as follows:
 # Step 3: Create pmd_oclint.xml
oclint-json-compilation-database -- -max-priority-1 '1' -max-priority-2 '50' -max-priority-3 '100' -report-type pmd -o pmd_oclint.xml

Save that script and build Jenkins CI again. The successful result as below:

Tools: XCode 7.x, OCLint 0.10.1
Ref documents:- OCLint.org

Comments

Popular posts from this blog

So sánh những framework hỗ trợ viết ứng dụng trên SmartPhone

Khi lập trình trên SmartPhone bạn không nhất thiết phải học những ngôn ngữ đặc thù trên từng loại hệ điều hành thì mới có thể lập trình được. Ví dụ như muốn lập trình trên iOS thì phải học ngôn ngữ Objective-C hay Swift, muốn lập trình được trên Android thì học ngôn ngữ Java, muốn lập trình trên WinPhone thì học ngôn ngữ C#. Hiện nay có rất nhiều những framework giúp đỡ cho các bạn rất nhiều khi các bạn muốn viết trên nhiều nền tảng smartphone bằng ngôn ngữ mà bạn yêu thích. Theo mình thấy thì hiện nay có 3 loại như: Native App, Hybrid Mobile App, Native Cross-Platform App. 1. Native App: là những ứng dụng sử dụng những framework và ngôn ngữ lập trình của hệ thống cung cấp sẵn. Ví dụ như bạn muốn lập trình iOS thì phải cài XCode, học ngôn ngữ Objective-C hay Swift, lập trình Android thì cài Android Studio và học ngôn ngữ Java. - Ưu điểm: Hiệu năng thực thi ứng dụng trên nền tảng nhanh và hiệu quả. Không bị phụ thuộc vào bên thứ 3. Khi phát hành ứng dụng trên những Mobile Store cũng dễ...

Hướng dẫn dùng Serverless sử dụng Lambda AWS

1. Lambda function là gì? AWS Lambda cho phép bạn chạy mã mà không cần cung cấp hay quản lý máy chủ. Bạn chỉ phải trả tiền cho thời gian xử lý thông tin đã sử dụng. Với Lambda, bạn có thể chạy mã cho gần như toàn bộ các loại ứng dụng hay dịch vụ backend – tất cả đều không cần quản trị. Chỉ cần tải đoạn mã của bạn lên và Lambda sẽ lo hết những gì cần làm để chạy và mở rộng mã của bạn với mức độ có sẵn cao. Bạn có thể thiết lập mã của bạn tự động kích hoạt từ các dịch vụ AWS khác, hoặc gọi trực tiếp từ bất cứ ứng dụng web hay di động nào. Chi phí chạy trên lambda function rẻ so với chi phí bạn mua 1 con server, duy trì và quản trị nó ( ví dụ như bạn phải xử lý bất đồng bộ những request, khi lượng user bạn tăng đột biến bạn phải có cơ chế auto scale, chứ không thì server bị sẽ bị treo, khi server bị treo bạn phải tự động khởi động lại sẽ mất thời gian,... ).

Phân biệt biến kiểu Property, Public, Protected, Private trong ngôn ngữ Objective C

- Theo kinh nghiệm làm việc của mình với các bạn trong nhóm khi lập trình Objective-C và cũng đọc qua code của những project cũ. Ít khi nào mọi người để ý và khai báo đúng với ý đồ của từng đối tượng, và vi phạm quy tắc tính đóng gói, tính bảo mật thông tin của đối tượng trong lập trình hướng đối tượng (Tham khảo lý thuyết Lập trình hướng đối tượng tại trang Wiki ). - Theo ngôn ngữ lập trình Java, người ta khuyến khích mỗi khi dùng biến kiểu public thì nên đặt 1 biến private và hỗ trợ những hàm getter/setter để truy suất biến private đó.     + Nguyên nhân họ nói là đảm bảo tính đóng gói, và nếu sau này có thay đổi gì trên biến đó bạn có thể sửa được dễ dàng, chi tiết về vấn đề này ở đây .     + Nói tóm tại thì nguyên nhân chính là có thể kiểm soát được truy xuất đến giá trị của 1 đối tượng từ bên ngoài, có thể dễ dàng mở rộng code bằng cách override lại những hàm getter/setter. - Các bạn có thể áp dụng nguyên tắc đó từ bên Java qua ngôn ngữ lập trình Object...